In: Computer Science
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following:
1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0.
2. Immediately after computing individual student average, add a statement that will accumulate the newly computed average using the variable from previous step.
3. After the outer loop for the number of students, add a statement that will compute class average as the accumulated averages divided by the number of students.
4. Finally, display the computed class average. Do not use computations inside output statements because the computed value might be needed with later updates.
Save and submit your final code file as Pr5-13_Lab.cpp.
// This program uses a nested loop to average
// a set of test scores for multiple students.
#include <iostream>
using namespace std;
int main()
{
int numStudents, // Number of students
numTests; // Number of tests per student
double average; // Average test score for a student
// Get the number of students
cout << "This program averages test scores.\n";
cout << "How many students are there? ";
cin >> numStudents;
// Get the number of test scores per student
cout << "How many test scores does each student have? ";
cin >> numTests;
cout << endl;
// Read each student's scores and compute their average
for (int snum = 1; snum <= numStudents; snum++) //Outer loop
{ double total = 0.0; // Initialize accumulator
for (int test = 1; test <= numTests; test++) //Inner loop
{ int score;
// Read a score and add it to the accumulator
cout << "Enter score " << test << " for ";
cout << "student " << snum << ": ";
cin >> score;
total += score;
} //End inner loop
// Compute and display the student's average
average = total / numTests;
cout << "The average score for student " << snum;
cout << " is " << average << "\n\n";
} //End outer loop
return 0;
}
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
// This program uses a nested loop to average
// a set of test scores for multiple students.
#include <iostream>
using namespace std;
int main()
{
int numStudents, // Number of students
numTests; // Number of tests per student
double average; // Average test score for a student
double sum_average = 0; //Initialize accumulator for calculating class average
double class_average; //Average class score
// Get the number of students
cout << "This program averages test scores.\n";
cout << "How many students are there? ";
cin >> numStudents;
// Get the number of test scores per student
cout << "How many test scores does each student have? ";
cin >> numTests;
cout << endl;
// Read each student's scores and compute their average
for (int snum = 1; snum <= numStudents; snum++) //Outer loop
{
double total = 0.0; // Initialize accumulator
for (int test = 1; test <= numTests; test++) //Inner loop
{
int score;
// Read a score and add it to the accumulator
cout << "Enter score " << test << " for ";
cout << "student " << snum << ": ";
cin >> score;
total += score;
} //End inner loop
// Compute and display the student's average
average = total / numTests;
cout << "The average score for student " << snum;
cout << " is " << average << "\n\n";
//accumulating the student average to sum_average
sum_average += average;
} //End outer loop
//computing class average as the accumulated averages divided by the number of students.
class_average = sum_average/numStudents;
cout << "The class average score is ";
cout << class_average << "\n\n";
return 0;
}
OUTPUT: