Grading on a Curve Program, wrong output when two values are 0.

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.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace 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';
					
					else if ( x[p] >= mean + (standard_dev / 2) )
						lgrade[p] = 'B';
				
					else if ( x[p] >= mean - (standard_dev / 2) )
						lgrade[p] = 'C';
					
					else if ( 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!!
When iterating over an array index must be always < sizeof array.

So this for ( j = 0; j <= n; j++ ) -> for ( j = 0; j < n; j++ )
and this for ( k = 0; k <= n; k++ ) -> for ( k = 0; k < n; k++ )
Topic archived. No new replies allowed.