My C++ code compiles, but I realized after I finished that I need to make the us
ID: 3553185 • Letter: M
Question
My C++ code compiles, but I realized after I finished that I need to make the user choose what row and column to get the row total, column total, highest in a row, and lowest in a row.
What do I change to add that into this code?
#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()
{
int testArray[][COLS] =
{
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20 }
};
cout << "The total of the array elements is "
<< getTotal(testArray, ROWS, COLS)
<< endl;
cout << "The average value of an element is "
<< getAverage(testArray, ROWS, COLS)
<< endl;
cout << "The total of row 0 is "
<< getRowTotal(testArray, 0, COLS)
<< endl;
cout << "The total of col 2 is "
<< getColumnTotal(testArray, 2, ROWS)
<< endl;
cout << "The highest value in row 2 is "
<< getHighestInRow(testArray, 2, COLS)
<< endl;
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[][C
Explanation / Answer
int main()
{
int value;
int testArray[][COLS] =
{
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20 }
};
cout << "The total of the array elements is "
<< getTotal(testArray, ROWS, COLS)
<< endl;
cout << "The average value of an element is "
<< getAverage(testArray, ROWS, COLS)
<< endl;
cout<<" enter the value of row to calculate the total";
cin>>value;
cout << "The total of entered row is "
<< getRowTotal(testArray, value, COLS)
<< endl;
cout<<" enter the value of column to calculate the total";
cin>>value;
cout << "The total of entered col is "
<< getColumnTotal(testArray, value, ROWS)
<< endl;
cout<<" enter the value of row to calculate the highest value";
cin>>value;
cout << "The highest value in entered row is "
<< getHighestInRow(testArray, value, COLS)
<< endl;
cout<<" enter the value of row to calculate the lowest value";
cin>>value;
cout << "The lowest value in eneterd row is "
<< getLowestInRow(testArray, value, COLS)
<< endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.