In: Computer Science
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods:
-getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.
-getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array.
-getRowTotal. This method 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 method should return the total of the values in the specified row.
-getColumnTotal. This method 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 method should return the total of the values in the specified column.
-getHighestInRow. This method 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 method should return the highest value in the specified row of the array.
-getLowestInRow. This method 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 method should return the lowest value in the specified row of the array. Demonstrate each of the methods in this program.
C++ CODE:
#include<iostream>
using namespace std;
int getTotal(int arr[3][4])
{
int total=0,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
total+=arr[i][j];
}
}
return total;
}
double getAverage(int arr[3][4])
{
double average=0;
int total=0,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
total+=arr[i][j];
}
}
average=total/(3.0 * 4);
return average;
}
int getRowTotal(int arr[3][4],int r)
{
int total=0,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if (i==r)
total+=arr[i][j];
}
}
return total;
}
int getColumnTotal(int arr[3][4],int c)
{
int total=0,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if (j==c)
total+=arr[i][j];
}
}
return total;
}
int getHighestInRow(int arr[3][4],int r)
{
int total=0,i,j;
int max=INT_MIN;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if (i==r)
{
if (arr[i][j]> max)
{
max=arr[i][j];
}
}
}
}
return max;
}
int getLowestInRow(int arr[3][4],int r)
{
int total=0,i,j;
int min=INT_MAX;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if (i==r)
{
if (arr[i][j]< min)
{
min=arr[i][j];
}
}
}
}
return min;
}
int main()
{
int arr[3][4]={
{1,2,3,4},
{2,3,4,5},
{1,7,8,9},
};
int t=getTotal(arr);
cout<<"total is "<<t;
double a=getAverage(arr);
cout<<"\nAverage is "<<a;
int rt=getRowTotal(arr,0);
cout<<"\nrow total is "<<rt;
int ct=getColumnTotal(arr,0);
cout<<"\ncolumn total is "<<ct;
int m=getHighestInRow(arr,2);
cout<<"\n Max value in row is "<<m;
int mi=getLowestInRow(arr,2);
cout<<"\n Min value in row is "<<mi;
return 0;
}
SCREENSHOT FOR OUTPUT: