Averaging Grades

Hey guys if somebody shortens this code. I think is too long for me.
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
  #include <iostream>
#include <cmath>
using namespace std;

int main() {
	float Filipino, Computer, Mathematics, English, Science, Average;
	
	cout << "This is a Average Grade Calculator" << endl;
	cout << "A = 98-90, B = 85-89, C = 80-84, D = 75-79, F = 70 below" << endl;
	cout << "Input Grade Filipino: " << endl;
	cin >> Filipino;
	cout << "Input Grade Computer: " << endl;
	cin >> Computer;
	cout << "Input Grade Mathematics: " << endl;
	cin >> Mathematics;
	cout << "Input Grade English: " << endl;
	cin >> English;
	cout << "Input Grade Science: " << endl;
	cin >> Science;
	Average = (Filipino + Computer + Mathematics + English + Science) / 5;
	cout << "Average Grade is; " << Average << endl;
	cout << "Grade is: ";
		
	if ((Average >= 90) && (Average <= 98)) {
		cout << "A" << endl;
	}
	else if ((Average >= 85) && (Average <= 89)) {
		cout << "B" << endl;
	}
	else if ((Average >= 80) && (Average <= 84)) {
		cout << "C" << endl;
	}
	else if ((Average >= 75) && (Average <= 79)) {
		cout << "D" << endl;
	}
	else {
		cout << "F" << endl;
	}
	return 0;
}
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
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	float Filipino, Computer, Mathematics, English, Science, Average;

	cout << "This is a Average Grade Calculator" << endl;
	cout << "A = 98-90, B = 85-89, C = 80-84, D = 75-79, F = 70 below" << endl;
	cout << "Input Grade Filipino: " << endl;
	cin >> Filipino;
	cout << "Input Grade Computer: " << endl;
	cin >> Computer;
	cout << "Input Grade Mathematics: " << endl;
	cin >> Mathematics;
	cout << "Input Grade English: " << endl;
	cin >> English;
	cout << "Input Grade Science: " << endl;
	cin >> Science;
	Average = (Filipino + Computer + Mathematics + English + Science) / 5;
	cout << "Average Grade is; " << Average << endl;
	cout << "Grade is: ";

	if (Average < 99)
	{
		if (Average > 89) { cout << "A" << endl; }
		else if (Average > 84) { cout << "B" << endl; }
		else if (Average > 79) { cout << "C" << endl; }
		else if (Average > 74) { cout << "D" << endl; }
		else { cout << "F" << endl; }
	}
	else { cout << "Impossible" << endl; }
	
	return 0;
}


If you look at your original code there are potential undefined values as Average can fall through the gaps being a float. For example if Average == 89.5 it satisfies no requirement as 89.5 is both less than 90 and greater than 89 thus you will not get a grade printed.
Also consider the data types used for the other variables as you seem to want whole numbers for all inputs and need to consider negative entries and decimal ones also.

You could also use a switch if you so desired. But basically what i hope to demonstrate here is the unnecessary checks performed on trying to get a range value when you can do a single check to get expected results. The updated code takes into account float values (although you may need to redefine your expected grades for decimal numbers. for example is 84.9 considered 'B'? as your original code says it needs to be 85 at least)

Alternatively you can truncate the result to an int or unsigned int to get rid of the decimal value and round down.
Last edited on
somebody shortens this code. I think is too long for me


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
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	const char* const subs[] = {"Fillipino", "Computer", "Mathematics", "English", "Science"};

	float total {}, grade {};

	cout << "This is a Average Grade Calculator\n";
	cout << "A = 98-90, B = 85-89, C = 80-84, D = 75-79, F = 70 below\n";

	for (const auto& sub : subs) {
		cout << "Input Grade for " << sub << ": ";
		cin >> grade;
		total += grade;
	}

	const float Average {total / size(subs)};

	cout << "Average Grade is " << Average << '\n' << "Grade is ";

	if (Average < 99) {
		if (Average > 89) cout << "A\n";
		else if (Average > 84) cout << "B\n";
		else if (Average > 79) cout << "C\n";
		else if (Average > 74) cout << "D\n";
		else cout << "F" << endl;
	} else
		cout << "Impossible\n";
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using namespace std;

struct Boundary{ int min; char grade; };
vector<Boundary> boundaries = { { 90, 'A' }, { 85, 'B' }, { 80, 'C' }, { 75, 'D' }, { 0, 'F' } };

int main()
{
   const int N = 5;
   double sum = 0, mark;
   cout << "Enter your grades for Filipino, Computing, Mathematics, English, Science:\n";
   for ( int i = 0; i < N; i++ )
   {
      cin >> mark;
      sum += mark;
   }
   double average = sum / N;
   int GPA = average + 0.5;
   int p = 0;
   while ( boundaries[p].min > GPA ) p++;
   cout << "Average mark = " << average << "      Grade = " << boundaries[p].grade << '\n';
}
Topic archived. No new replies allowed.