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

Write a program that creates a two-dimensional array initialized with test data.

ID: 3546206 • 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.

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

- getColumTotal. This function should accept a two-dimensional array as its 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 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 of array.

- getLowestInRow. This function should accept a two-dimensional array as its 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 of array.


Here is the sample ouput:


testArray[0][0]: 1

tesArray [0][1]: 2

tesArray [0][2]: 3

tesArray [0][3]: 4

tesArray [0][4]: 5

tesArray [1][0]: 6

tesArray [1][1]: 7

tesArray [1][2]: 8

tesArray [1][3]: 9

tesArray [1][4]: 10

tesArray [2][0]: 11

tesArray [2][1]: 12

tesArray [2][2]: 13

tesArray [2][3]: 14

tesArray [2][4]: 15

tesArray [3][0]: 16

tesArray [3][1]: 17

tesArray [3][2]: 18

tesArray [3][3]: 19

tesArray [3][4]: 20


The Total of the array elements is 210

The average value of an element is 10.5

The total of row 0 is 15

The total of col 2 is 42

The higest value in row 2 is 11

Press any key to continue...





Explanation / Answer

Dear,


/********************************************************
* Program demonstrates creating two-dimensional        *
* array and tests the data.                            *
********************************************************/

/********************************************************
* Methods used: getTotal(),getAverage(),getRowTotal() *
*               getColumnTotal,getHighestInRow(),         *
*               getLowestInRow().                              *
******************************************/


//Header file section
# include <iostream>

Define 2 rows and 2 columns

#define NUM_ROWS 2
#define NUM_COLS 2
using namespace std;

Create functions

//Declare functions
int getTotal(int[][NUM_COLS]);
double getAverage(int);
int getRowTotal(int[][NUM_COLS],int);
int getColumnTotal(int[][NUM_COLS],int);
double getAverage(int);
int getHighestInRow(int[][NUM_COLS],int);
int getLowestInRow(int[][NUM_COLS],int);

//Start main
int main ()
{
    //Declare varaiable
    int i,j,value,ch;   

Insert four elements into matrix

    int num[NUM_ROWS][NUM_COLS]={10,20,30,40};
    cout<<"The Matrix elements are: "<<endl;

    //Display the elements
    for(i=0;i<NUM_ROWS;i++)
     {
       
       for(j=0;j<NUM_COLS;j++)
       cout<<num[i][j]<<" ";
       cout<<endl;
     }

Enter the user data

    //Shows the following functions
     for(;;)
    {
    cout<<" Your choices are: "<<endl;
    cout<<"1 get Total: ";
    cout<<"2 get Average: ";
    cout<<"3 get Row Total ";
    cout<<"4 get Column Total ";
    cout<<"5 get Highest valueue In a Row ";
    cout<<"6 get the Lowest valueue In a Row ";
    cout<<"7 Exit ";
    cout<<"Enter your choice: ";
    cin>>ch;
   
//Start choice case

    //Start switch case for calling functions
    switch(ch)
     {

//Calling functions
       case 1: cout<<"Total of all valueues= "
                    <<getTotal(num)<<endl;
             break;
     



case 2: value=getTotal(num);
             cout<<"The average= "
                 <<getAverage(value)<<endl;
             break;
      case 3: cout<<"Enter row number:";
             cin>>i;
             if(i<0||i>=NUM_ROWS)
             {
            cout<<"Error-must have between 0 and"
                  <<NUM_ROWS<<" Try it again: ";
                  break;
             }
            
             else
               cout<<"Row "<<i<<" total value="
                   <<getRowTotal(num,i)<<endl;
             break;

      case 4: cout<<"Enter column number:";
             cin>>i;
             if(i<0||i>=NUM_COLS)
             {
            cout<<"Error must have between 0 and"
                  <<NUM_COLS<<" Try it again. ";
                break;
             }
             else
                cout<<"Column "<<i<<" total value="
                    <<getColumnTotal(num,i)<<endl;
             break;

      case 5: cout<<"Enter row number:";
             cin>>i;
             if(i<0||i>=NUM_ROWS)
            {
                 cout<<"Error must have between 0 and"
                     <<NUM_ROWS<<" Try it again. ";
                  break;
                  }
             else
                  cout<<"Highest element in row "<<i<<"= "
                      <<getHighestInRow(num,i)<<endl;
             break;
      case 6: cout<<"Enter row number: ";
             cin>>i;
             if(i<0||i>=NUM_ROWS)
             {
            cout<<"Error must must have between 0 and"
                  <<NUM_ROWS<<" Try it again.";
               break;
             }
             else
                  cout<<"Lowest element in row "<<i<<"= "
                      <<getLowestInRow(num,i)<<endl;
             break;
      case 7: system("pause");
             return 0;              
      default: cout<<"Error!! TryAgain ";
      }
}

}

This function accepts a two-dimensional array as its argument and returns total value

/*This function returns the total
of all the values in the array.*/
int getTotal(int row[][NUM_COLS])
{
    //Declare variables
int i,j,value=0;

//Calculate total elements values
for(i=0;i<NUM_ROWS;i++)
     for(j=0;j<NUM_COLS;j++)
   value+=row[i][j];
return value;
}

This function accepts a two-dimensional array as its argument and returns average value

/*This function returns the average
of all the values in the array.*/
double getAverage(int totalValues)
{
    double avg;
    avg=totalValues/(NUM_ROWS*NUM_COLS);
    return avg;
}

This function accepts a two-dimensional array as its first argument and an integer as its second argument and returns total value in the specified row.

/*This function returns the total of
values in the specified row.*/
int getRowTotal(int num[][NUM_COLS],int n)
{
//Declare variables
int i,value=0;

//Calculate specified row total value
for(i=0;i<NUM_COLS;i++)
   value+=num[n][i];
return value;
}

This function accepts a two-dimensional array as its first argument and an integer as its second argument and returns total value in the specified column.

/*This function returns the total of
values in the specified column.*/
int getColumnTotal(int num[][NUM_COLS],int n)
{
//Declare variables
int i,value=0;

//Calcute specified column total
for(i=0;i<NUM_ROWS;i++)
   value+=num[i][n];
return value;
}

This function accepts a two-dimensional array as its first argument and an integer as its second argument and returns highest value in the specified row.

/*This function returns the highest
value in the specified row.*/
int getHighestInRow(int num[][NUM_COLS],int n)
{
    //Declare variables
    int i,value=0;
   value=num[n][0];
//Calculate highest value in specified row
   for(i=1;i<NUM_COLS;i++)
   if(num[n][i]>value)
      value=num[n][i];
return value;
}

This function accepts a two-dimensional array as its first argument and an integer as its second argument and returns lowest value in the specified row.

/*This function returns the lowest
value in the specified row.*/
int getLowestInRow(int num[][NUM_COLS],int n)
{
//Declare variables
    int i,value=0;
value=num[n][0];
//Calculate lowest value in specified row
for(i=1;i<NUM_COLS;i++)
   if(num[n][i]<value)
      value=num[n][i];
return value;
}

Hope this will help you!!


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