Question
Write a program that creates a two-dimensional array (2D array) initialized with test data. The program should have the following functions: getTotal. This function should
Write a program that creates a two-dimensional array (2D array) initialized with test data. 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 value 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 thehighest 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.
#include
using namespace std;
// Constant for the number of columns
const int COLS = 5;
// Function prototypes
int getTotal(int [][COLS], int);
double getAverage(int [][COLS], int);
int getRowTotal(int [][COLS], int);
int getColumnTotal(int [][COLS], int, int);
int getHighestInRow(int [][COLS], int);
int getLowestInRow(int [][COLS], int);
int main()
{
const int ROWS = 4; // Constant for the number of rows
// Array with test data
int testArray[ROWS][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)
<< endl;
// Display the average of the array elements.
cout << "The average value of an element is "
<< getAverage(testArray, ROWS)
<< endl;
// Display the total of row 0.
cout << "The total of row 0 is "
<< getRowTotal(testArray, 0)
<< 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)
<< endl;
// Display the lowest value in row 2.
cout << "The lowest value in row 2 is "
<< getLowestInRow(testArray, 2)
<< endl;
system("PAUSE");
return 0;
}
// ********************************************************
// The getTotal function returns the total of all *
// the elements in the array. *
// ********************************************************
int getTotal(int array[][COLS], int rows)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getAverage function returns the averave value *
// of the elements in the array. *
// ********************************************************
double getAverage(int array[][COLS], int rows)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getRowTotal function returns the total of the *
// the elements in the specified row of the array. *
// ********************************************************
int getRowTotal(int array[][COLS], int rowToTotal)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getColTotal function returns the total of the *
// the elements in the specified column of the array. *
// ********************************************************
int getColumnTotal(int array[][COLS], int colToTotal, int rows)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getHighestInRow function returns the highest *
// value in the specified row. *
// ********************************************************
int getHighestInRow(int array[][COLS], int rowToSearch)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
// ********************************************************
// The getLowestInRow function returns the lowest *
// value in the specified row. *
// ********************************************************
int getLowestInRow(int array[][COLS], int rowToSearch)
{
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
// IMPLEMENT THIS FUNCTION
}
Annotations
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started