In: Computer Science
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;
}
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 :