Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Use 2D arrays: 1. Write a program that creates and programmatically initiali

ID: 3702504 • Letter: C

Question

C++

Use 2D arrays:
1. Write a program that creates and programmatically initializes a two-dimensional array
with test data. Use any numeric data type you wish. The program should implement the
following functions:
a. getTotal – This function should accept a two-dimensional array as its argument
and return the total of all the values in the array.
b. getAverage – This function should accept a two-dimensional array as its
argument and return the average of all the values in the array.
c. 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.
d. 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.
e. getHighestInRow – 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.
f. getLowestInRow – 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.
2. Your main() function should demonstrate each of the above functions.

Explanation / Answer

#include <iostream>
using namespace std;

int getTotal(int arr[4][4]);
int getAverage(int arr[4][4]);
int getRowTotal(int arr[4][4],int rno);
int getColumnTotal(int arr[4][4],int cno);
int getHighestInRow(int arr[4][4], int rno);
int getLowestInRow(int arr[4][4], int rno);
int main()
{
int array[4][4] =
{
{2, 5, 3, 0},
{4, 0 ,5 ,3},
{9, 1, 7 ,5},
{3, 6, 8, 3},
};
int total = getTotal(array);
cout<<"total of all the values is" << total << " ";
  
float average = getAverage(array);
cout<<"Average of all the values is" << average << " ";
  
int rownumber;
cout<<"enter row number 0, 1 , 2 or 3";
cin>> rownumber;
int rowtotal= getRowTotal(array, rownumber);
cout<<"Total of all the values in a given row is " << rowtotal << " ";

  
int columnnumber;
cout<<"enter columnnumber 0, 1 , 2 or 3";
cin>> columnnumber;
int columntotal= getColumnTotal(array, columnnumber);
cout<<"Total of all the values in a given column is " << columntotal << " ";

int rownumberforhighest;
cout<<"enter row number 0, 1 , 2 or 3";
cin>> rownumberforhighest;
int highest= getHighestInRow(array, rownumberforhighest);
cout<<"highest number in a row is " << highest << " ";

int rownumberforlowest;
cout<<"enter row number 0, 1 , 2 or 3";
cin>> rownumberforlowest;
int lowest= getLowestInRow(array, rownumberforlowest);
cout<<"Lowest number in a row is " << lowest << " ";

  
  
return 0;
}

int getTotal(int arr[4][4])
{
int sum;
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
{
sum= sum + arr[i][j];
}
}
return sum;
  
}
int getAverage(int arr[4][4])
{
  
float sum;
float avg;
  
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
{
sum= sum + arr[i][j];
}
}
avg= sum/16.0;
return avg;
  
}


int getRowTotal(int arr[4][4],int rno)
{
int rtotal;
  
  
for(int j = 0; j < 4; ++j)
{
rtotal= rtotal + arr[rno][j];
}
  
return rtotal;
  
  
}
int getColumnTotal(int arr[4][4],int cno)
{
int ctotal;
  
  
for(int i = 0; i < 4; ++i)
{
ctotal= ctotal + arr[i][cno];
}
  
return ctotal;
  
}
int getHighestInRow(int arr[4][4], int rno)
{
int max=arr[rno][0];
for(int j = 0; j < 4; ++j)
{
if(max<arr[rno][j])//max should not be lower than any number in a row
{ max= arr[rno][j];
}
}
return max;

}
int getLowestInRow(int arr[4][4], int rno)
{
int min=arr[rno][0];
for(int j = 0; j < 4; ++j)
{
if(min>arr[rno][j])//min should not be greater than any number in a row
{ min= arr[rno][j];
}
}
return min;
}

output

total of all the values is64
Average of all the values is4
enter row number 0, 1 , 2 or 30
Total of all the values in a given row is 10
enter columnnumber 0, 1 , 2 or 31
Total of all the values in a given column is 12
enter row number 0, 1 , 2 or 31
highest number in a row is 5
enter row number 0, 1 , 2 or 31
Lowest number in a row is 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote