In: Computer Science
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in your 2D array. 3. A function called “column_average” that returns the average of a specified column in your 2D array. 4. A function called “row_average” that returns the average of a specified row in your 2D array. 5. A function called “sort_row” that sorts a specified row in your 2D array. 6. A function called "search" that does a linear search on a user specified row and returns the columns index where it was found. If it wasn't found, return -1. Make sure you pass in the value to be search for.. Create a menu system that enables the user to call each of the above functions. For 3 and 4 allow the user to enter the column or row they wish to see the averages of. For 5, allow the user to specify the row to be sorted and ask them if they would like to see the min and max element of that row. The user should be able to enter ‘Y’, ‘y’, ‘N’, or ‘n’. Make sure to validate all user input. If they entered an invalid value, don’t ask them for a valid input and just pass zero to your function. For 6, ask the user for the row and value (space separated) to be searched.
Sample output:
Program code to copy:
#include <iostream>
using namespace std;
int sum(int arr[][50]);
float average(int arr[][50]);
float column_average(int arr[][50], int col);
float row_average(int arr[][50], int row);
void sort_row(int arr[][50], int row);
int search(int arr[][50], int num, int row);
int main()
{
// an array with 100 rows and 50 columns.
int x[100][50];
int range = (75 + 75) + 1;
int choice, col, row, num, index;
char ch;
srand(25);
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
x[i][j] = rand()%range - 75;
}
}
while (1) {
cout<<"1. Sum\n";
cout<<"2. Average\n";
cout<<"3. Column Average\n";
cout<<"4. Row Average\n";
cout<<"5. Sort Row\n";
cout<<"6. Search Row\n";
cout<<"7. Exit\n";
cout<<"\n Your choice ?";
cin>>choice ;
switch ( choice) {
case 1 :
cout<< "\nThe sum is: " << sum(x) <<endl;
break ;
case 2 :
cout<< "\nThe average is: " << average(x)
<<endl;
break ;
case 3 :
cout<< "\nEnter column number: ";
cin>>col ;
if ((col < 0) || (col > 99))
col = 0;
cout << "\n The average of column " << col << "
is " << column_average(x, col) << endl;
break;
case 4 :
cout<< "\nEnter row number: ";
cin>>row ;
if ((row < 0) || (row > 49))
row = 0;
cout << "\n The average of row " << row << "is "
<< row_average(x, row) << endl;
break;
case 5 :
cout<< "\nEnter row number: ";
cin>>row ;
if ((row < 0) || (row > 49))
row = 0;
sort_row(x, row);
cout << "Do you want to see the minimum and maximum elements
of row " << row << "?\n";
cout << "Enter y or n : ";
cin >> ch;
while ((ch != 'Y') && (ch != 'y') && (ch != 'N')
&& (ch != 'n')){
cout << "Wrong input!! Enter y or n : ";
cin >> ch;
}
if ((ch == 'Y') || (ch == 'y')){
cout << "Maximum: " << x[row][49] << endl;
cout << "Minimum: " << x[row][0] << endl <<
endl;
}
break;
case 6:
cout<< "\nEnter row number & element to search: ";
cin>>row ;
cin>>num ;
if ((row < 0) || (row > 49))
row = 0;
if ((num < -75) || (num > 75))
num = 0;
index = search(x, num, row);
if(index == -1){
cout << "Element is not found." << endl;
}
else {
cout << "Element " << num <<" is found at index "
<< index << endl;
}
break;
case 7 :
exit(1) ;
}
}
return 0;
}
int sum(int arr[][50]){
int total = 0;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
total += arr[i][j];
}
}
return total;
}
float average(int arr[][50]){
int total = 0;
float average = 0;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
total += arr[i][j];
}
}
average = total/(100.0*50.0);
return average;
}
float column_average(int arr[][50], int col){
int total = 0;
float average = 0;
for (int i = 0; i < 100; i++)
{
total += arr[i][col];
}
average = total/100.0;
return average;
}
float row_average(int arr[][50], int row){
int total = 0;
float average = 0;
for (int i = 0; i < 50; i++)
{
total += arr[row][i];
}
average = total/50.0;
return average;
}
void sort_row(int arr[][50], int row){
int min, min_index;
for(int i=0; i < 50; i++){
min = arr[row][i];
for(int j=i; j < 50; j++){
if(min > arr[row][j]){
min = arr[row][j];
min_index = j;
}
}
if(i != min_index){
arr[row][min_index] = arr[row][i];
arr[row][i] = min;
}
}
cout << "Sorted row is: "<< endl;
for (int i = 0; i < 50; i++)
{
cout << arr[row][i] << " ";
}
cout << endl;
}
int search(int arr[][50], int num, int row){
int index = -1;
for (int i = 0; i < 50; i++)
{
if (num == arr[row][i]){
index = i;
break;
}
}
return index;
}