Write a program that creates a two-dimensional array initialized with test data.
ID: 3937671 • 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: This function should accept a two-dimensional array as its argument and return the total of all the values in the array. This function should accept a two-dimensional array as its a and return the average of all the values in the array. This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should subscript of a row in the array. The function should return the total of the values in the specified row. This function should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should subscript of a column in the array. The function should return the total of the values in the specified column. This function should accept a two-dimensional array as its first argument and an integer as its sExplanation / Answer
#include <iostream>
using namespace std;
const int row=3,col=3;
int getTotal(int array[][col])
{
int total=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
total+=array[i][j];
}
}
return total;
}
double getAverage(int array[][col])
{
int total=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
total+=array[i][j];
}
}
return (double)total / (row*col);
}
int getRowTotal(int array[][col], int row)
{
row=row-1;
int total=0;
for(int j=0;j<col;j++)
{
total+=array[row][j];
}
return total;
}
int getColTotal(int array[][col], int col)
{
col=col-1;
int total=0;
for(int j=0;j<row;j++)
{
total+=array[j][col];
}
return total;
}
int getHighestInRow(int array[][col], int row)
{
row=row-1;
int high=array[row][0];
for(int j=1;j<col;j++)
{
if(array[row][j]>high)
{
high=array[row][j];
}
}
return high;
}
int getLowestInRow(int array[][col], int row)
{
row=row-1;
int low=array[row][0];
for(int j=1;j<col;j++)
{
if(array[row][j]<low)
{
low=array[row][j];
}
}
return low;
}
int main()
{
int array[][col]={{2,4,5},{7,1,9},{5,2,3}};
int total=getTotal(array);
cout<<endl<<"Total: "<<total;
double avg=getAverage(array);
cout<<endl<<"Average: "<<avg;
cout<<endl<<"Total of elements in 2nd row: "<<getRowTotal(array,2); //specify 1,2,3
cout<<endl<<"Total of elements in 1st column: "<<getColTotal(array,1); //specify 1,2,3
cout<<endl<<"Highest element in 3rd row: "<<getHighestInRow(array,3); //specify 1,2,3
cout<<endl<<"Lowest element in 2rd row: "<<getHighestInRow(array,2); //specify 1,2,3
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.