I need help creating this code pg 460 & 462 2D Array Operations The program shou
ID: 3621023 • Letter: I
Question
I need help creating this code pg 460 &462
2D Array Operations
The program should have the following functions:
• getTotal. This function should accept a two-dimensional array as its argument
and return the total of all the values in the array.
• getAverage. This function should accept a two-dimensional array as its argument
and return the average of all the values in the array.
• getRowTotal. This function should accept a two-dimensional array as its first
argument and an integer as its second argument. The second argument should be
the subscript of a row in the array. The function should return the total of the values
in the specified row.
• getColurnnTotal. This function should accept a two-dimensional array as its first
argument and an integer as its second argument. The second argument should be
the subscript of a column in the array. The function should return the total of the
values in the specified column.
• getHighestlnRow. This function should accept a two-dimensional array as its
first argument and an integer as its second argument. The second argument
should be the subscript of a row in the array. The function should return the highest
value in the specified row of the array.
• getLowestlnRow. This function should accept a two-dimensional array as its first
argument and an integer as its second argument. The second argument should be
the subscript of a row in the array. The function should return the lowest value in
the specified row of the array.
Demonstrate each of the functions in this program
use this data for the program
Test Score Letter Grade
90-100 A
80-89 B
70-79 c
60-69 D
0-59 F
Explanation / Answer
please rate - thanks
CRAMSTER rule is 1 question per post,
you mention 2 questions, define 1 and give data for the other. here is the one you defined
# include <iostream>
#define NROW 4
#define NCOL 5
using namespace std;
int getTotal(int[][NCOL]);
int getRowTotal(int[][NCOL],int);
int getColumnTotal(int[][NCOL],int);
double getAverage(int);
int getLowestInRow(int[][NCOL],int);
int getHighestestInRow(int[][NCOL],int);
int main ()
{int i,j,choice;
int num[NROW][NCOL]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},val;
cout<<"The Matrix ";
for(i=0;i<NROW;i++)
{for(j=0;j<NCOL;j++)
cout<<num[i][j]<<" ";
cout<<endl;
}
for(;;)
{cout<<"Choose what you would like to do ";
cout<<"1 get Total of all values in matrix: ";
cout<<"2 get Average of all values in matrix: ";
cout<<"3 get a Row Total ";
cout<<"4 get a Column Total ";
cout<<"5 get the Highest value In a Row ";
cout<<"6 get the Lowest value In a Row ";
cout<<"7 Exit ";
scanf("%d",&choice);
switch(choice)
{case 1: cout<<"Total= "<<getTotal(num)<<endl;
break;
case 2: val=getTotal(num);
cout<<"The average is "<<getAverage(val)<<endl;
break;
case 3: cout<<"Enter row : ";
cin>>i;
if(i<0||i>=NROW)
{cout<<"Error-must be between 0 and "<<NROW<<" ";
break;
}
else
cout<<"Row "<<i<<" total is "<<getRowTotal(num,i)<<endl;
break;
case 4: cout<<"Enter column: ";
cin>>i;
if(i<0||i>=NCOL)
{cout<<"Error-must be between 0 and "<<NCOL<<" ";
break;
}
else
cout<<"Column "<<i<<" total is "<<getColumnTotal(num,i)<<endl;
break;
case 5: cout<<"Enter row : ";
cin>>i;
if(i<0||i>=NROW)
{cout<<"Error-must be between 0 and "<<NROW<<" ";
break;
}
else
cout<<"Highest element in row "<<i<<" is "<<getHighestestInRow(num,i)<<endl;
break;
case 6: cout<<"Enter which row : ";
cin>>i;
if(i<0||i>=NROW)
{cout<<"Error-must be between 0 and "<<NROW<<" ";
break;
}
else
cout<<"Lowest element in row "<<i<<" is "<<getLowestInRow(num,i)<<endl;
break;
case 7: system("pause");
return 0;
default: cout<<"Error!! Try Again ";
}
}
}
int getTotal(int r[][NCOL])
{int i,j;
int val=0;
for(i=0;i<NROW;i++)
for(j=0;j<NCOL;j++)
val+=r[i][j];
return val;
}
int getHighestestInRow(int num[][NCOL],int n)
{int i;
int val=0;
val=num[n][0];
for(i=1;i<NCOL;i++)
if(num[n][i]>val)
val=num[n][i];
return val;
}
double getAverage(int t)
{return (double)t/(NROW*NCOL);
}
int getLowestInRow(int num[][NCOL],int n)
{int i;
int val=0;
val=num[n][0];
for(i=1;i<NCOL;i++)
if(num[n][i]<val)
val=num[n][i];
return val;
}
int getRowTotal(int num[][NCOL],int n)
{int i;
int val=0;
for(i=0;i<NCOL;i++)
val+=num[n][i];
return val;
}
int getColumnTotal(int num[][NCOL],int n)
{int i;
int val=0;
for(i=0;i<NROW;i++)
val+=num[i][n];
return val;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.