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: 3554986 • 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.

Explanation / Answer

#include <iostream>
#include <string>

#define ROWS 4
#define COLS 5

using namespace std;

int getTotal(int[][COLS], int, int);
float getAverage(int[][COLS], int, int);
int getRowTotal(int[][COLS], int, int);
int getColumnTotal(int[][COLS], int, int);
int getHighestInRow(int[][COLS], int, int);
int getLowestInRow(int[][COLS], int, int);

int main()
{
 // Array with test data
 int testArray[][COLS] =
 {
  { 1, 2, 3, 4, 5 },
  { 6, 7, 8, 9, 10 },
  { 11, 12, 13, 14, 15 },
  { 16, 17, 18, 19, 20 }
 };

 // Display the total of the array elements.
 cout << "The total of the array elements is "
 << getTotal(testArray, ROWS, COLS)
 << endl;
 
 // Display the average of the array elements.
 cout << "The average value of an element is "
 << getAverage(testArray, ROWS, COLS)
 << endl;
 
 // Display the total of row 0.
 cout << "The total of row 0 is "
 << getRowTotal(testArray, 0, COLS)
 << endl;
 
 // Display the total of column 2.
 cout << "The total of col 2 is "
 << getColumnTotal(testArray, 2, ROWS)
 << endl;
 
 // Display the highest value in row 2.
 cout << "The highest value in row 2 is "
 << getHighestInRow(testArray, 2, COLS)
 << endl;
 
 // Display the lowest value in row 2.
 cout << "The lowest value in row 2 is "
 << getLowestInRow(testArray, 2, COLS)
 << endl;
 
 return 0;
}

int getTotal(int array[][COLS], int rows, int cols)
{
 int sum = 0;
 for(int i = 0; i < rows; i++)
 {
  for(int j = 0; j < cols; j++)
  {
   sum = sum + array[i][j];
  }
 }
 return sum;
}

float getAverage(int array[][COLS], int rows, int cols)
{
 int count = rows*cols; /* number of items in 2D array */
 int sum = getTotal(array, rows, cols);
 float average = (float)sum /(float)count;
 return average;
}

int getRowTotal(int array[][COLS], int the_row, int cols)
{
 int sum = 0;
 for(int i = 0; i < cols; i++)
 {
  sum = sum + array[the_row][i]; 
 }
 return sum;
}

int getColumnTotal(int array[][COLS], int the_col, int rows)
{
 int sum = 0;
 for(int i = 0; i < rows; i++)
 {
  sum = sum + array[i][the_col];
 }
 return sum;
}

int getHighestInRow(int array[][COLS], int the_row, int cols)
{
 /* assume the first is highest and then change it as you check */
 int highest = array[the_row][0];
 for(int i = 0; i < cols; i++)
 {
  if(array[the_row][i] > highest)
  {
   highest = array[the_row][i];
  }
 }
 return highest;
}

int getLowestInRow(int array[][COLS], int the_row, int cols)
{
 /* assume the first is lowest and then change it as you check */
 int lowest = array[the_row][0];
 for(int i = 0; i < cols; i++)
 {
  if(array[the_row][i] < lowest)
  {
   lowest = array[the_row][i];
  }
 }
 return lowest;
}

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