### Coding using vector

Hi,i'm still new for c++ programming..can anyone help me with this coding..this is the question..

Write C++ program that reads student scores, finds the best score, and then assign grades based on the following scheme:

Grade is A if score is >= best – 10; Grade is D if score >= 40;
Grade is B if score is >= best – 20; Grade is F otherwise.
Grade is C if score is >= best – 30;

The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores, and concludes by displaying the grades. Below shows a sample run:

Enter the number of students: 4
Enter 4 scores: 40 55 70 58
Student 1 score is 40 and grade is C
Student 2 score is 55 and grade is C
Student 2 score is 55 and grade is A
Student 2 score is 55 and grade is B

This is the code so far:

#include <iostream>
#include <vector>

using namespace std;

int main() {
double num, best;
vector <int> score;
cout<<"Enter the number of students: "; cin>>num;
cout<<"Enter "<<num<<" scores: ";

for (int i = 0; i < num; i++) {
cin>>score[i];}

best = score[0];
for (int i = 0; i < num; i++) {
if (score[i] > best) best = score[i];}

for (int i = 0; i < num; i++) {
if (score[i] >= -10) grade[i] = 'A';
else if (score[i] >= -20) grade[i] = 'B';
else if (score[i] >= -30) grade[i] = 'C';
else if (score[i] >= -40) grade[i] = 'D';

for (int i = 0; i < num; i++) {
}

return 0;
}
Last edited on
Look at the following snippet.
`if (score[i] >= -10) grade[i] = 'A';`
Does this code fulfill the assignment specifications?
 Grade is A if score is >= best – 10;

Please use code tags when posting code, and you also need to ask a specific question in the body of your post.
jlb, i donn't exactly understand what you're trying to say..can u explain it..
Last edited on
This is a shorter version of your code:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````// Write C++ program that reads student scores, finds the best score, and then // assign grades based on the following scheme: // Grade is A if score is >= best – 10; Grade is D if score >= 40; // Grade is B if score is >= best – 20; Grade is F otherwise. // Grade is C if score is >= best – 30; // The program prompts the user to enter the total number of students, then // prompts the user to enter all of the scores, and concludes by displaying // the grades. Below shows a sample run: // Enter the number of students: 4 // Enter 4 scores: 40 55 70 58 // Student 1 score is 40 and grade is C // Student 2 score is 55 and grade is C // Student 2 score is 55 and grade is A // Student 2 score is 55 and grade is B #include #include void waitForEnter(); int main() { std::cout<<"Enter the number of students: "; int num = 0; std::cin >> num; std::cout << "Enter " << num << " scores: "; std::vector scores; int best = 0; for (int i = 0; i < num; i++) { int tmp = 0; std::cin >> tmp; scores.push_back(tmp); if(tmp > best) { best = tmp; } } for (int i = 0; i < num; i++) { std::cout << "Student " << i + 1 << " score is " << scores.at(i) <<" and grade is "; if (scores.at(i) >= -10) { std::cout << "A\n"; } else if (scores.at(i) >= -20) { std::cout << "B\n"; } else if (scores.at(i) >= -30) { std::cout << "C\n"; } else if (scores.at(i) >= -40) { std::cout << "D\n"; } else { std::cout << "F\n"; } } return 0; }``````

I think what jlb is telling you is you aren't respecting the assignment specifications when you evaluate the grades starting from the scores. You're missing a point; just read them more carefully.

 i donn't exactly understand what you're trying to say..can u explain it..

In your if statements where are you do the computations?
 // Grade is A if score is >= best – 10; Grade is D if score >= 40; // Grade is B if score is >= best – 20; Grade is F otherwise. // Grade is C if score is >= best – 30;

And you still haven't actually asked a question. Maybe "u explain it", the problem that is.
 ``123456789101112131415161718192021`` ``````#include #include using namespace std; int main() { char grade[] = { 'A', 'B', 'C', 'D', 'F' }; int num; cout << "How many students? "; cin >> num; valarray score( num ), rank( num ); cout << "Enter " << num << " scores\n"; for ( int i = 0; i < num; i++ ) cin >> score[i]; int best = score.max(); rank = ( best - score - 1 ) / 10; for ( int &r : rank ) if ( r > 4 ) r = 4; for ( int i = 0; i < num; i++ ) cout << "Student: " << i + 1 << " score: " << score[i] << " grade: " << grade[rank[i]] << '\n'; }``````

You are ignoring "best", your condition for grade 'D' is inconsistent (I'm guessing in the code above) and your model answer doesn't make sense.