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

Here is the problem: Write a program that creates a two-dimensional array initia

ID: 3622262 • Letter: H

Question

Here is the problem:

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.

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.

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 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.

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.

The following was given to me as a study guide but I do not understand it. It compiles but I need explination. Please rework this so that there are explinations for the steps and the letters used as variables are changed to meaningful names. I want to understand this which is why I am asking for all of the explination.

Thanks for your time.

 

#include<iostream>
#include <conio.h>
using namespace std;
#define row 4
#define col 5

int getTotal(int[][col]);
double getAverage(int[][col]);
int getRowTotal(int[][col],int);
int getColumnTotal(int[][col],int);
int getHighestRow(int[][col],int);
int getLowestRow(int[][col],int);

int main()
{int n=2;
int mat[row][col]={1,2,3,4,5,
       6,7,8,9,10,
       11,12,13,14,15,
       16,17,18,19,20};
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: "<<getHighestRow(mat,n)<<endl;
cout<<"The lowest element of row "<<n<<" is: "<<getLowestRow(mat,n)<<endl;
_getch();
return 0;

int getTotal(int a[][col])
   {int i,j,sum=0;
    for(i=0;i<row;i++)
       for(j=0;j<col;j++)
          sum+=a[i][j];
return sum;
}
double getAverage(int a[][col])
    {int sum=getTotal(a);
        return(double)sum/(row*col);
    }
int getRowTotal(int a[row][col],int n)
   {int i,sum=0;
   for(i=0;i<col;i++)
       sum+=a[n][i];
   return sum;
   }
int getColumnTotal(int a[][col],int n)
   {int i,sum=0;
   for(i=0;i<row;i++)
       sum+=a[i][n];
   return sum;
   }
int getHighestRow(int a[][col],int n)
   {int i,high;
     high=a[n][0];
     for(i=1;i<col;i++)
        if(a[n][i]>high)
             high=a[n][i];
    return high;
}
int getLowestRow(int a[][col],int n)
{int i,low;
     low=a[n][0];
     for(i=1;i<col;i++)
        if(a[n][i]<low)
             low=a[n][i];
    return low;
}

Explanation / Answer

please rate - thanks

my version of the program fully annotated

#include <iostream>
#include <iomanip>
#define MAXROWS 4
#define MAXCOLS 5
using namespace std;
void Print2DArray( const int [] [MAXCOLS] );
int getTotal(int[][MAXCOLS]);
int getRowTotal(int[][MAXCOLS],int);
int getColumnTotal(int[][MAXCOLS],int);
double getAverage(int);
int getLowestInRow(int[][MAXCOLS],int);
int getHighestInRow(int[][MAXCOLS],int);
int getLowestInCol(int[][MAXCOLS],int);
int getHighestInCol(int[][MAXCOLS],int);
int main ()
{int i,val;
double avg;
int num[MAXROWS][MAXCOLS]={1,2,3,4,5,
                           6,7,8,9,10,
                           11,12,13,14,15,
                           16,17,18,19,20};
Print2DArray(num); //print 2 dimensional array
val=getTotal(num); //add all elements in the array
cout<<"The Total for all the elements in this array is: "<<val<<endl;
cout<<"The Average of all the elements in this array is: "<<getAverage(val)<<endl;
cout<<"The sum of each row is: ";    
for(i=0;i<MAXROWS;i++)          //print sum of each row
cout<<"Row "<<i+1<<": "<<getRowTotal(num,i)<<endl;     
cout<<"The sum of each column is: ";
for(i=0;i<MAXCOLS;i++)         //print sum of each column
cout<<"Col "<<i+1<<": "<<getColumnTotal(num,i)<<endl;     
cout<<"The h" lowest-"ighest and lowest of each row is: ";
for(i=0;i<MAXROWS;i++)        //get and print highest and lowest in each row
cout<<"Row "<<i+1<<": highest: "<<getHighestInRow(num,i)
        <<" lowest: "<<getLowestInRow(num,i)<<endl;     
cout<<"The highest and lowest of each column is: ";
for(i=0;i<MAXCOLS;i++)    //get and print highest and lowest in each column
cout<<"Row "<<i+1<<": highest: " <<getHighestInCol(num,i)
       <<" lowest: "<<getLowestInCol(num,i)<<endl;
  
system("pause");
}

void Print2DArray( const int ary[] [MAXCOLS] )
{int i,j;
cout<<"The Matrix Column";
for(i=1;i<=MAXCOLS;i++)      //label and underline the columns
cout<<setw(7)<<i;
cout<<"           ";
for(i=1;i<=MAXCOLS;i++)
    cout<<" -----";
cout<<endl;
for(i=0;i<MAXROWS;i++)
   {cout<<"Row "<<i+1<<":   ";     //print the row number at the beginning of the line
   for(j=0;j<MAXCOLS;j++)          //print each column valuein that row
       cout<<setw(7)<<ary[i][j];
    cout<<endl;                   //when finish row go to the next line
    }
cout<<endl;   
}
int getTotal(int r[][MAXCOLS])
{int i,j;
int total=0;                           //start total at 0 (additive identity
for(i=0;i<MAXROWS;i++)                  //for each row
     for(j=0;j<MAXCOLS;j++)
   total+=r[i][j];                     //add all the values
return total;
}
int getHighestInRow(int num[][MAXCOLS],int n)
{int i;
int max;
max=num[n][0];   //assume row n column 0 is the highest in the row
for(i=1;i<MAXCOLS;i++)      //starting with column index 1,
   if(num[n][i]>max)        //if any are larger
      max=num[n][i];        //make it the largest
return max;                 //return the largest
}
int getHighestInCol(int num[][MAXCOLS],int n)
{int i;
int max;   
max=num[0][n];       //assume column n row 0 is the highest in the column
for(i=1;i<MAXROWS;i++)     //starting with row index 1,
   if(num[i][n]>max)        //if any are larger
      max=num[i][n];         //make it the largest
return max;                  //return the largest
}
double getAverage(int t)
{return (double)t/(MAXROWS*MAXCOLS);    //average=total of all elements/# of elements
}
int getLowestInRow(int num[][MAXCOLS],int n)
{int i;
int min;
min=num[n][0];       //assume row n column 0 is the lowest in the row
for(i=1;i<MAXCOLS;i++)    //starting with column index 1,
   if(num[n][i]<min)      //if any are smaller  
      min=num[n][i];      //make it the smallest
return min;               //return the smallest
}
int getLowestInCol(int num[][MAXCOLS],int n)
{int i;
int min;
min=num[0][n];            //assume row 0 column n is the lowest in the clumn
for(i=1;i<MAXROWS;i++)      //starting with row index 1,
   if(num[i][n]<min)        //if any are smaller  
      min=num[i][n];       //make it the smallest
return min;                //return the smallest
}
int getRowTotal(int num[][MAXCOLS],int n)
{int i;
int total=0;                    //initialize total to 0
for(i=0;i<MAXCOLS;i++)          //for each column in row n
   total+=num[n][i];           //add the elements
return total;                  //and return the total
}
int getColumnTotal(int num[][MAXCOLS],int n)
{int i;
int total=0;                    //initialize total to 0
for(i=0;i<MAXROWS;i++)          //for each row in column n
total+=num[i][n];              //add the elements
return total;                   //and return the total   
}