Write a C++ program that manage the student’s grades. Starting with a fixed number of students (10)

I would need some help writing this code, I have one made but having trouble getting it to work, any help is appreciated. Basically the number of grades is given by the user. The grades are stored in an array. Two functions are called for each student. One function will give the numeric average of their grades. The other function will give a letter grade to that average. Grades are assigned on a 10-point spread.
90-100 A
80- 89 B
70-79 C
60-69 D
Below 60 F

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

int main() {
	int n;
	char gradesArray[10];
	cout << "Enter the number of grades: ";
	cin >> n;
	double scores[10][n];

	for (int i = 0; i < 10; i++)
	{
		cout << "Enter the scores for student " << i + 1 << "\n";
		for (int j = 0; j < n; j++)
			cin >> scores[i][j];
		double av = avg(scores[i], n);
		gradesArray[i] = grades(av);
	}
	cout << "The grades are \n";
	for (int i = 0; i < 10; i++)
		cout << "Student " << i + 1 << " " << gradesArray[i] << "\n";
	return 0;
}

double avg(double arr[], int n)
{
	double s = 0;
	for (int i = 0; i < n; i++)
		s += arr[i];
	s = s / n;
	return s;
}
char grades(double score)
{
	char grade;
	if (score >= 90)
		grade = 'A';
	else if (score >= 80)
		grade = 'B';
	else if (score >= 70)
		grade = 'C';
	else if (score >= 60)
		grade = 'D';
	else
		grade = 'F';
	return grade;
}
Last edited on
Hello MyMom420,

I think you forgot about "p" and "q".

On line 9 you are trying to create a VLA, (Variable Length Array), this is not proper C++ code. The size of the areray needs to be known at compile time, so the compiler will know how much space to set aside for the array. Some compilers may allow the use of VLAs, but do not count on this.

You could replace the array "scores" with a 2D vector and you would not have to worry about how much or little is entered. You can also set up the vector with a beginning size if needed.

In the function "grades" you are comparing a "double" to an "int". This may work, but not the best approach. Consider: if (score >= 90.0). At least this way the 2 types will match.


I have one made but having trouble getting it to work


Be more specific. Not getting what to work?

Andy
As the number of grades per student is determined at run-time and not compile time, a c-style array cannot be used to store these. A vector is needed. Consider:

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

double avg(const vector<double>& arr);
char grades(double score);

int main() {
	const int noStud {10};
	char gradesArray[noStud] {};
	int n {};

	cout << "Enter the number of grades: ";
	cin >> n;

	for (int i = 0; i < noStud; ++i) {
		vector<double> scores(n);

		cout << "Enter the " << n << " scores for student " << i + 1 << '\n';
		for (int j = 0; j < n; ++j)
			cin >> scores[j];

		gradesArray[i] = grades(avg(scores));
	}

	cout << "\nThe grades are \n";
	for (int i = 0; i < noStud; ++i)
		cout << "Student " << i + 1 << " " << gradesArray[i] << "\n";
}

double avg(const vector<double>& arr)
{
	double s {};

	for (const double v : arr)
		s += v;

	return s / arr.size();
}

char grades(double score)
{
	char grade {};

	if (score >= 90.0)
		grade = 'A';
	else if (score >= 80.0)
		grade = 'B';
	else if (score >= 70.0)
		grade = 'C';
	else if (score >= 60.0)
		grade = 'D';
	else
		grade = 'F';

	return grade;
}


However, the avg() function is not required and it's not required to store the scores just to find the average. Consider:

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

char grades(double score);

int main() {
	const int noStud {10};
	char gradesArray[noStud] {};
	int n {};

	cout << "Enter the number of grades: ";
	cin >> n;

	for (int i = 0; i < noStud; ++i) {
		double total {};

		cout << "Enter the " << n << " scores for student " << i + 1 << '\n';
		for (int j = 0; j < n; ++j) {
			double score {};

			cin >> score;
			total += score;
		}

		gradesArray[i] = grades(total / n);
	}

	cout << "\nThe grades are \n";
	for (int i = 0; i < noStud; ++i)
		cout << "Student " << i + 1 << " " << gradesArray[i] << "\n";
}

char grades(double score)
{
	char grade {};

	if (score >= 90.0)
		grade = 'A';
	else if (score >= 80.0)
		grade = 'B';
	else if (score >= 70.0)
		grade = 'C';
	else if (score >= 60.0)
		grade = 'D';
	else
		grade = 'F';

	return grade;
}

Topic archived. No new replies allowed.