Write a program that creates a two-dimensional array initialized with test data.
ID: 3849308 • Letter: W
Question
Write a program that creates a two-dimensional array initialized with test data. Use any data type you wish. 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. get Average. This function should accept a two-dimensional array as its argument and return the average of all the values in the array. get Row Total. This function should accept a two-dimensional array as it 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. getColumnTotal. 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. getHighestInRow. ThExplanation / Answer
Program
#include<iostream>
using namespace std;
#define row 4
#define col 5
// Declaration
int getTotal(int[][col]);
double getAverage(int[][col]);
int getRowTotal(int[][col],int);
int getColumnTotal(int[][col],int);
int getHighestInRow(int[][col],int);
int getLowestInRow(int[][col],int);
int main()
{
int n=3; // Initialize "n" for taking either row number or column number
int mat[row][col]={1,2,3,4,5,
6,7,8,9,10,
11,12,13,14,15,
16,17,18,19,20}; // Take any type of row,column set in an array
// Printing return values of each function ( you can print which operation you want by putting comments for other lines)
cout<<"The total of all the values of the array is: "<<getTotal(mat)<<endl;
cout<<"the average of all the values in the array is: "<<getAverage(mat)<<endl;
cout<<"The total of row "<<n<<" is: "<<getRowTotal(mat,n)<<endl;
cout<<"The total of column "<<n<<" is: " <<getColumnTotal(mat,n) <<endl;
cout<<"The highest element of row "<<n<<" is: "<<getHighestInRow(mat,n)<<endl;
cout<<"The lowest element of row "<<n<<" is: "<<getLowestInRow(mat,n)<<endl;
return 0;
}
// getTotal function definition
int getTotal(int a[][col])
{
int i,j,sum=0;
for(i=0;i<row;i++) // for loop for itirations
for(j=0;j<col;j++)
sum+=a[i][j]; // Sum of all the elements in an array, executs as sum = sum + a[i][j]
return sum;
}
// getAverage function definition
double getAverage(int a[][col])
{
int sum=getTotal(a);
double avg = (sum)/(row*col); // In our case avg = (sum)/(4*5)
return avg;
}
int getRowTotal(int a[row][col],int rownumber)
{
int i,row_total=0;
for(i=0;i<col;i++) // since,we need rows based on Columns
row_total+=a[rownumber][i];
return row_total; // return value stored in row_total variable
}
// Function definition
int getColumnTotal(int a[][col],int columnnumber)
{
int i,column_total=0;
for(i=0;i<row;i++) // since,we need columns based on rows
column_total+=a[i][columnnumber];
return column_total;
}
// function definition
int getHighestInRow(int a[][col],int rownumber)
{
int i,high;
high=a[rownumber][0];
for(i=1;i<col;i++) // since,we need rows based on Columns
if(a[rownumber][i]>high)
high=a[rownumber][i];
return high;
}
// Function definition
int getLowestInRow(int a[][col],int rownumber)
{
int i,low;
low=a[rownumber][0];
for(i=1;i<col;i++) // since,we need rows based on Columns
if(a[rownumber][i]<low)
low=a[rownumber][i];
return low;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.