Question

In: Computer Science

For this C++ program, Write and modify the code to compute and display the class average...

For this C++ program, Write and modify the code to compute and display the class average as well as the standard deviation. Your code changes are as follows:

1. The variable “double grade” should be replaced by a two-dimensional array variable “double grade[NUMSTUDENTS][NUMGRADES].” Also replace the variable “double average” by “double average[NUMSTUDENTS].” This is necessary since you need to save the entered grades specially to compute the standard deviations.

2. After completing the display of the average grade of all students, your program should compute the class average (i.e., the average of four students’ average grades) and display.

3. Determine and display the class standard deviation by subtracting the class average from each student’s average grade (that results in a set of new numbers, each called a deviation); squaring each deviation; adding the squared deviations; dividing the sum by the number of students, NUMSTUDENTS; and taking its square root.

#include <iostream>

using namespace std;

int main()

{

const int NUMGRADES = 3;

const int NUMSTUDENTS = 4;

int i,j;

double grade, total, average;

for (i = 0; i < NUMSTUDENTS; i++) // start of outer loop

{

    total = 0;                       // clear the total for this student

    for (j = 0; j < NUMGRADES; j++) // start of inner loop

    {

      cout << "Enter an examination grade for this student: ";

      cin >> grade;

      total += grade;               // add the grade into the total

    }                                // end of the inner for loop

    average = total / NUMGRADES;     // calculate the average

    cout << "\nThe average for student " << i

      << " is " << average << "\n\n";

}                                  // end of the outer for loop

return 0;

}

Solutions

Expert Solution

Solution :

Following is the commented C++ program for the above problem :

#include <iostream>
#include<math.h>
using namespace std;

int main()

{

const int NUMGRADES = 3;

const int NUMSTUDENTS = 4;

int i,j;

double grade[NUMSTUDENTS][NUMGRADES], total, average[NUMSTUDENTS];

for (i = 0; i < NUMSTUDENTS; i++) // start of outer loop

{

    total = 0;                       // clear the total for this                                 // student

    for (j = 0; j < NUMGRADES; j++) // start of inner loop

    {

      cout << "Enter an examination grade for this student: ";

      cin >> grade[i][j];

      total += grade[i][j];      // add the grade into the total

    }                                // end of the inner for loop

    average[i] = total / NUMGRADES;     // calculate the average

    cout << "\nThe average for student " << i

      << " is " << average[i] << "\n\n";

}                   // end of the outer for loop
double class_average,class_total=0;
//compute class average
for(int i=0;i<NUMSTUDENTS;i++)
{
    class_total=class_total+average[i];
}
class_average= (class_total/NUMSTUDENTS);
cout<<"\n The average for the whole class is "<<class_average<<"\n";

//compute the standard deviation of the class
double class_deviation,class_square_sum=0;
for(int i=0;i<NUMSTUDENTS;i++)
{
    class_square_sum=class_square_sum+((class_average-average[i])*(class_average-average[i]));
}

class_deviation=sqrt(class_square_sum/NUMSTUDENTS);
cout<<"\n The standard deviation for the whole class is "<<class_deviation<<"\n";
return 0;
}

Code demo for reference :

Output :


Related Solutions

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...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
Write a C++ program to display toy name
Write a C++ program to display toy name
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...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
Write a C++ program to input 10 scores (range from 0-100), calculate the average, then display...
Write a C++ program to input 10 scores (range from 0-100), calculate the average, then display the student's name, and a letter grade such as A, B, C, D, or F. It shall have a Student class with necessary private data members and constructor and public methods, such as getName, getGrade, calcScore, convertToGrade, etc. Try to create a Student object in main(), then invoke its methods to perform the tasks.
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...
Write a program in C++ to display the following patterns: * ** *** **** 1 12...
Write a program in C++ to display the following patterns: * ** *** **** 1 12 123 1234 1 22 333 4444 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 * *** ***** ******* ********* ******* ***** *** *
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class...
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument . • setRadius. A mutator function for the radius variable. • getRadius. An acccssor function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT