finding average in loop

I'm trying to find the average of some grades in a loop. However if the grade is a zero I don't want that included in the average.

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
  int main()
{
	double totalGrade = 0.0;
	int grade=0;
	double average = 0.0;
	int allGrades = 0;
	int totalGrades = 0;
	char l;
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	int f = 0;
	
	while((l=getLetter())!='Q')
	{
		grade = getGrade();
		switch(l)
		{
			case 'A':
				grade+=grade;
			a++;
		break;
			case 'B':
				grade+=grade;
				b++;
				
				break;
			case 'C':
				grade+=grade;
				c++;
				
				break;
			case 'D':
				grade+=grade;
				d++;
				
				break;
			case 'F':
				grade+=grade;
				
					f++;	
				break;
		}
		
	}
	
	std::cout<<a<<b<<c<<d<<f<<endl;
	allGrades=a+b+c+d+f;
	totalGrades=allGrades;
	std::cout<<totalGrades<<endl;
	std::cout<<grade<<endl;
}
There are no number grades in that code. Anyways, I assume you just want to omit all the F's and just average the numerical value of the remaining letter grades. Since you already counted how many of each letter grade, you can just do something like:
 
average = (90*a + 80*b + 70*c + 60*d)/(a+b+c+d)


This excludes all the F's and gets you the average. Of course, you might want to change the values of each letter grade to something that makes more sense.
Last edited on
Sorry I left out some the function for the number grades.
1
2
3
4
5
6
7
8
int getGrade()
{
	int grade= 0;
	std::cout<< "-1 to quit"<<endl;
	std::cout << "what is grade"<<endl;
	std::cin >> grade;
	return grade;
}
Topic archived. No new replies allowed.