loading...

CS201 Assignment 2 Solution Spring 2017

You will need to use a two-dimensional array with five rows and five columns for this assignment.

 

Task 1:

            First of all, you will need to populate this 5×5 array with random numbers from 1 to 100. For this purpose create a function named populateArray() which will load the two-dimensional array with random numbers from 1 to 100.

 

Task 2:

            Create a function named displayArray() which will display the contents of this multi-dimensional array on the console as shown in the sample output.

 

Task 3:

            The final task is to find the maximum and minimum number in this array and display it on the console. For this purpose create a function findMaxMinNumber().

Sample output:

 

Solution :

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void populateArray(int array[5][5])
{
 int ab[5][5];
 srand(time(NULL));
 for(int i = 0; i < 5; i++)
 {
 for(int j = 0; j < 5; j++)
 {
 int random = rand() % 100 + 1;
 array[i][j] = random;
 }
 }
}
void displayArray(int array[5][5])
{
 cout << "\nDispalying Arrays Data......\n";
 
 for(int i = 0; i < 5; i++)
 {
 for(int j = 0; j < 5; j++)
 {
 
 cout << array[i][j] << "\t"; 
 }
 cout << endl;
 }
}
void FindMaxMinNumber(int array[5][5])
{
 int min = array[0][0];
 int max = min;
 for(int i = 0; i < 5; i++)
 {
 for(int j = 0; j < 5; j++)
 {
 if(array[i][j] < min)
 {
 min = array[i][j];
 }
 
 if(array[i][j] > max)
 {
 max = array[i][j];
 }
 }
 }
 cout << "\nMaximum Value In Array : " << max;
 cout << "\nMinimum Value In Array : " << min;
}
int main()
{
 int array[5][5];
 populateArray(array);
 displayArray(array);
 FindMaxMinNumber(array);
}

 

DOWNLOAD SOLUTION HERE
loading...