Question
I need help with this program. I am able to get rowise totals and averages but I am not able to get columnwise totals and
I need help with this program. I am able to get rowise totals and averages but I am not able to get columnwise totals and averages. When you fix the code for columnwise totals and averages, please provide a line by line explanation of the loop required to get the columnwise total and average. Please use inline comments if you have to. I don't just need the solution, but I also need the explanation.
/*2D Arrays- //Write a program that displays the grades and average for 4 students in 5 tests in a tabluar format
Output should looks like this just as I would see in an excel sheet:
Test1 Test2 Test3 Test4 Test5 Average
Student 1:
Student 2:
Student 3:
Student 4:
Average :
*/
#include
#include
#include
#include
#include
using namespace std;
int main()
{
const int SIZE = 7;
string titleArray[SIZE] = { " ", "Test1", "Test2", "Test3", "Test4", "Test5", "Average" };
//Now these elements from the list are stored in the titleArray. All I have to do is display them using a for loop with a space between them
for (int i = 0; i < SIZE; i++)
cout << titleArray[i] << " ";
cout << endl;
const int row = 4, col = 5;
int gradesArray[row][col];
srand(time(NULL));
double sum = 0.0, average = 0.0;
for (int i = 0; i < row; i++)
{
cout << "Student " << i << ": ";
for (int j = 0; j < col; j++)
{
int randNum = (rand() % (100 - 0 + 1)) + 0;
gradesArray[i][j] = randNum;
cout << left << setw(5) << gradesArray[i][j] << " ";
sum = sum + gradesArray[i][j];
}
average = double(sum) / col;
cout << setprecision(2) << fixed << showpoint;
cout << left << setw(5) << average;
cout << endl;
//Reset both sum and average to 0
sum = 0;
average = 0;
}
cout << "Average : "; //This will print the 1st item of the very last row
//Now we will compute columnwise sum and average
cout << setprecision(0) << fixed;
double total = 0.0, avg = 0.0;
for (int j = 0; j < col; j++)
{
int i;
for (i = 0; i < row; i++)
{
total = total + gradesArray[i][j];
}
for (int k = i; k < j; k++)
cout << left << setw(5) << total;
}
cout << endl << endl;
system("pause");
return 0;
}
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