Modify the code so it shows the average of each course and each student in a nic
ID: 3714712 • Letter: M
Question
Modify the code so it shows the average of each course and each student in a nicely formatted table.
#include <string> #include <iostream> #include <iomanip> using namespace std; int main() { string courses[] = {"Math","Science","English"}; string students[] = {"Joe","Jay"}; const int STUDENT_COUNT = sizeof(students) / sizeof(students[0]); const int COURSE_COUNT = sizeof(courses) / sizeof(courses[0]); short grades[COURSE_COUNT][STUDENT_COUNT]; float courseAverage[COURSE_COUNT] = { 0.0 }; for (size_t i = 0; i < COURSE_COUNT; i++){ cout << "Input grades for course: " << courses[i] << endl; for (size_t j = 0; j < STUDENT_COUNT; j++){ cout << " " << students[j] << ": "; cin >> grades[i][j]; } } for (size_t i = 0; i < COURSE_COUNT; i++) { courseAverage[i] = 0.0; for (size_t j = 0; j < STUDENT_COUNT; j++) { courseAverage[i] += grades[i][j]; } courseAverage[i] /= STUDENT_COUNT; } for (size_t i = 0; i < COURSE_COUNT; i++) { for (size_t j = 0; j < STUDENT_COUNT; j++) { cout << setw(6) << grades[i][j]; } cout << endl; } cout << "The Averages are: " << endl; for (size_t i = 0; i < COURSE_COUNT; i++) { cout << setw(15) << courses[i] << setw(8) << setprecision(2) << fixed << courseAverage[i] << endl; } return 0; }Explanation / Answer
/////// OUTPUT //////////////////
Input grades for course: Math
Joe: 80
Jay: 100
Input grades for course: Science
Joe: 90
Jay: 90
Input grades for course: English
Joe: 85
Jay: 90
Math Science English Average
Joe 80 90 85 85.00
Jay 100 90 90 93.33
-----------------------------------------------
Average 90.00 90.00 87.50 89.17
/////////////////////////////////Modified Program ////////////////////////////////////
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string courses[] = {"Math","Science","English"};
string students[] = {"Joe","Jay"};
const int STUDENT_COUNT = sizeof(students) / sizeof(students[0]);
const int COURSE_COUNT = sizeof(courses) / sizeof(courses[0]);
short grades[COURSE_COUNT][STUDENT_COUNT];
float studentAverage[STUDENT_COUNT] = { 0.0 };
float courseAverage[COURSE_COUNT] = { 0.0 };
float averageSum = 0.0f;
for (size_t i = 0; i < COURSE_COUNT; i++){
cout << "Input grades for course: " << courses[i] << endl;
for (size_t j = 0; j < STUDENT_COUNT; j++){
cout << " " << students[j] << ": ";
cin >> grades[i][j];
}
}
for (size_t course = 0; course < COURSE_COUNT; course++) {
courseAverage[course] = 0.0;
for (size_t student = 0; student < STUDENT_COUNT; student++) {
courseAverage[course] += grades[course][student];
}
courseAverage[course] /= STUDENT_COUNT;
averageSum += courseAverage[course];
}
for (size_t student = 0; student < STUDENT_COUNT; student++) {
studentAverage[student] = 0.0;
for (size_t course = 0; course < COURSE_COUNT; course++) {
studentAverage[student] += grades[course][student];
}
studentAverage[student] /= COURSE_COUNT;
averageSum += studentAverage[student];
}
cout << setw(15) << ""; //space for name
for (size_t course = 0; course < COURSE_COUNT; course++) {
cout << setw(10) << courses[course]; //space for name
}
cout << setw(10) << "Average"; //space for name
cout << endl;
for (size_t student = 0; student < STUDENT_COUNT; student++) {
cout << setw(15) << students[student]; //space for name
for (size_t course = 0; course < COURSE_COUNT; course++) {
cout << setw(10) << grades[course][student];
}
cout << setw(10) << setprecision(2) << fixed << studentAverage[student]; //space for name
cout << endl;
}
cout << setw(10) << "" << "-----------------------------------------------" << endl;
cout << setw(15) << "Average";
for (size_t course = 0; course < COURSE_COUNT; course++)
{
cout << setw(10) << setprecision(2) << fixed << courseAverage[course];
}
cout << setw(10) << setprecision(2) << fixed << averageSum / (COURSE_COUNT+STUDENT_COUNT);
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.