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

Write a c++ program that creates a two-dimensional array initialized with test d

ID: 3625962 • Letter: W

Question

Write a c++ program that creates a two-dimensional array initialized with test data. Use the following data:

78 85 99 56
79 77 68 79
89 90 60 74

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 the 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 specific 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 specific column.

*getHighestInRow. This function should accept two-dimensional arrays as its first argument and an integer as the 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 the array.

*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 of the array.

Demonstrate each of the functions in this program.

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