### 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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include using namespace std; int main() { int n; char gradesArray; cout << "Enter the number of grades: "; cin >> n; double scores[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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include using namespace std; double avg(const vector& 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 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& 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include 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; }``````

Registered users can post here. Sign in or register to post.