In: Computer Science
I need this in C++ please. I started it, but I need to add Get Average, Get Row total, and Get highest in row to my source code. But I keep getting build errors. Can someone please help me with my code. I attached my code after the question. Thank you!
Write a program that create a two-dimensional array initialized with test data. The program should have the following functions:
getTotal - This function should accept 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 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 in 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 in the array.
Demonstrate each of the function in this program.
my source code:
#include <iostream>
using namespace std;
const int ROWS = 4;
const int COLS = 5;
int getTotal(int [][COLS], int, int);
int getColumnTotal(int [][COLS], int, int);
int getLowestInRow(int [][COLS], int, int);
int main()
{
int paArray[ROWS][COLS] = { {1, 2, 3, 4, 5}, {6, 7, 8,
9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20} };
cout << "The total equals " <<
getTotal(paArray, ROWS, COLS) << "\n";
cout << "The total of column 3 is " <<
getColumnTotal(paArray, 3, ROWS) << "\n";
cout << "The lowest in row 3 is " <<
getLowestInRow(paArray, 3, COLS) << "\n";
system("pause");
return 0;
}
int getTotal(int array[][COLS], int rows, int cols)
{
int sum = 0;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols;
col++)
sum +=
array[row][col];
}
return sum;
}
int getColumnTotal(int array[][COLS], int colToTotal, int
rows)
{
int sum = 0;
for (int row = 0; row < rows; row++)
{
sum +=
array[row][colToTotal];
}
return sum;
}
int getLowestInRow(int array[][COLS], int rowToSearch, int
cols)
{
int lowest = array[rowToSearch][0];
for (int col = 1; col < cols; col++)
{
if (array[rowToSearch][col] <
lowest)
lowest =
array[rowToSearch][col];
}
return lowest;
}
C++ Program:
#include <iostream>
using namespace std;
const int ROWS = 4;
const int COLS = 5;
int getTotal(int [][COLS], int, int);
int getColumnTotal(int [][COLS], int, int);
int getLowestInRow(int [][COLS], int, int);
/* Adding function prototypes */
double getAverage(int [][COLS], int, int);
int getRowTotal(int [][COLS], int, int);
int getHighestInRow(int [][COLS], int, int);
int main()
{
int paArray[ROWS][COLS] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11,
12, 13, 14, 15}, {16, 17, 18, 19, 20} };
cout << "The total equals " << getTotal(paArray,
ROWS, COLS) << "\n";
cout << "The average equals " <<
getAverage(paArray, ROWS, COLS) << "\n";
cout << "The total of column 3 is " <<
getColumnTotal(paArray, 3, ROWS) << "\n";
cout << "The total of row 2 is " <<
getRowTotal(paArray, 2, ROWS) << "\n";
cout << "The lowest in row 3 is " <<
getLowestInRow(paArray, 3, COLS) << "\n";
cout << "The highest in row 2 is " <<
getHighestInRow(paArray, 2, COLS) << "\n";
system("pause");
return 0;
}
int getTotal(int array[][COLS], int rows, int cols)
{
int sum = 0;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
sum += array[row][col];
}
return sum;
}
int getColumnTotal(int array[][COLS], int colToTotal, int
rows)
{
int sum = 0;
for (int row = 0; row < rows; row++)
{
sum += array[row][colToTotal];
}
return sum;
}
int getLowestInRow(int array[][COLS], int rowToSearch, int
cols)
{
int lowest = array[rowToSearch][0];
for (int col = 1; col < cols; col++)
{
if (array[rowToSearch][col] < lowest)
lowest = array[rowToSearch][col];
}
return lowest;
}
//Function that calculates average
double getAverage(int array[][COLS], int rows, int cols)
{
//Calculating total of array
int total = getTotal(array, rows, cols);
//Calculating average
double avg = total/(double)(rows * cols);
//REturning average
return avg;
}
//Function that calculates row total
int getRowTotal(int array[][COLS], int rowToTotal, int cols)
{
//Initially set sum to 0
int sum = 0;
//Iterating over array
for (int col = 0; col < cols; col++)
{
//Accumulating sum
sum += array[rowToTotal][col];
}
//Return sum
return sum;
}
//Function that gets highest value in a row
int getHighestInRow(int array[][COLS], int rowToSearch, int
cols)
{
//Initially assume starting value is highest
int highest = array[rowToSearch][0];
//Iterating over each column
for (int col = 1; col < cols; col++)
{
//Comparing value
if (array[rowToSearch][col] > highest)
//Updating highest value
highest = array[rowToSearch][col];
}
//Returning highest value
return highest;
}
_________________________________________________________________________________________
Sample Run:
