Here is the problem:
PROBLEM: Letter grades are sometimes assigned to numeric scores by using the grading scheme commonly known as grading on the curve. In this scheme, a letter grade is assigned to a numeric score (see if-else statements inside the class function displayLetterGrade).
Write a program to read a list of real numbers representing numeric scores, call functions to calculate their mean and standard deviation, and then call a function to determine and display the letter grade corresponding to each numeric score. Use a class for the functions. Use file input output.
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
usingnamespace std;
class CurvedGrading {
private:
double mean, standard_dev;
public:
CurvedGrading(){
mean = 89.1;
standard_dev = 99.1;
}
double getMean( double x[], int n ) {
double sum2 = 0;
int j;
for ( j = 0; j <= n; j++ ) {
sum2 += x[j];
}
return mean = sum2 / n;
}
void computeStandardDev( double x[], int n ) {
double sum = 0, m;
int k;
m = getMean( x, n );
for ( k = 0; k <= n; k++ ) {
sum += pow( x[k] - m, 2.0 );
}
standard_dev = sqrt( sum / n );
}
void displayLetterGrade( double x[], int n ) {
ofstream print;
char lgrade[100];
int p;
print.open("p1.out");
if (print.is_open()){
print << "Mean: " << mean << "\nStandard Deviation: " << standard_dev << "\n\n";
print << "| STUDENT | NUMERIC_GRADE | LETTER_GRADE | \n";
for ( p = 0; p < n; p++ ) {
if ( x[p] >= mean + (3 * standard_dev / 2) )
lgrade[p] = 'A';
elseif ( x[p] >= mean + (standard_dev / 2) )
lgrade[p] = 'B';
elseif ( x[p] >= mean - (standard_dev / 2) )
lgrade[p] = 'C';
elseif ( x[p] >= mean - (3 * standard_dev / 2) )
lgrade[p] = 'D';
else
lgrade[p] = 'F';
print << setw(6) << p + 1 << setw(14) << x[p] << setw(14) << lgrade[p] << endl;
}
}
else
cout << "Error opening file. Output filename must be \"p1.out\"" << endl;
print.close();
}
};
int main() {
ifstream read;
CurvedGrading curving;
double scores[100];
int count = 0, i = 0;
read.open("p1.in");
if (read.is_open()){
while (read >> scores[i]){
count++;
i++;
}
curving.computeStandardDev( scores, count );
cout << "\nFile successfully opened and processed. Check \"p1.out\" for results.\n\n";
curving.displayLetterGrade( scores, count );
}
else
cout << "\nError opening file. Input filename must be \"p1.in\"\n\n";
read.close();
return 0;
}
The problem is that when I put two zero values inside the input file, it produces incorrect results. See this input and output below..
Input:
100 150 0 80 86 0 78 93 74 68
Output:
Mean: -1.#QNAN
Standard Deviation: -1.#QNAN
| STUDENT | NUMERIC_GRADE | LETTER_GRADE |
1 100 F
2 150 F
3 0 F
4 80 F
5 86 F
6 0 F
7 78 F
8 93 F
9 74 F
10 68 F
The "nan"/"qnan" appears all the time. I guess it has something to do with the input values of 0. But when I enter a single 0 value, it produces correct results. See the input and output below:
Input:
100 150 0 80 86 78 93 74 68
Output:
Mean: 81
Standard Deviation: 45.5107
| STUDENT | NUMERIC_GRADE | LETTER_GRADE |
1 100 C
2 150 A
3 0 F
4 80 C
5 86 C
6 78 C
7 93 C
8 74 C
9 68 C
How do I correct the output? Which part of the code needs some tweaking? Any suggestions are appreciated. Thanks in advance!!