Question

In: Computer Science

Modify the program 5-13 from page 279 such that will also compute the class average. This...

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;

}

Solutions

Expert Solution

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:


Related Solutions

[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall average test grade. E.g if there are 3 test each student must take and the user enters the following set of test scores for the two students…: 30, 40, 50 for the first student 50, 60, 70 for the second student …then program will print the average for each student (i.e. 40 for the first student and 60 for the second student – the...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the problem from using arrays to ArrayLists. There are some other modifications as well, so read the instructions carefully. The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an **ArrayList** of Strings to hold the user’s input –Use a do-while loop to prompt for and read the Strings (lines of text) from...
This is a Java program assignment. In the GradeCalculator class, compute the final average and letter...
This is a Java program assignment. In the GradeCalculator class, compute the final average and letter grade for a particular student. The final average is calculated according to the following rules: 1) There are ten exams scored out of 100 points 2) The lowest exam score is not included in the calculation of the final average 3) The highest exam score is not included in the calculation of the final average 4) An average of 91-100 is an A 5)...
Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following: 1. Add an overloaded...
Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following: 1. Add an overloaded constructor that has a parameter for radius. Negative values should result in radius set to 1.0. (see example 7-6 page 427) 2. Add a member function calcCircumference() to compute and return the circle circumference (2 * 3.14 * radius). 3. In main() "circle1" should be instantiated with a value for radius. // This program uses a constructor to initialize a member variable. #include <iostream>...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from the comments in the main routine that it wants an input argument of 3 to solve for a triangle and 4 to solve for a square or rectangle. MODIFY the code to add another method in the 'threeSides' and 'fourSides' classes to return the perimeter. ASSUME the following for TRIANGLES. ** for AREA (1/2 * base * height) dimension1 is base, dimension2 is height...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
IN C++ Modify the above program to compute the side area, total area, and volume of...
IN C++ Modify the above program to compute the side area, total area, and volume of a cylinder and the area and volume of a sphere, depending on the choice that the user makes. Your program should ask users to enter 1 to choose cylinder or 2 for sphere, and display an "invalid choice error" for other values. For a cylinder, we want to compute: Side area: (2*PI*r) * h Total Area: 2*(PI*r2) + Side area Volume: (PI*r2)*h For a...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables   ...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables    private long accountNumber;    private String firstName;    private String lastName;    private double balance;       //constructor    public Account(long accountNumber, String firstName, String lastName, double balance) {        this.accountNumber = accountNumber;        this.firstName = firstName;        this.lastName = lastName;        this.balance = balance;    }    //all getters and setters    public long getAccountNumber() {        return...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT