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;
vector <char> grade;
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';
else grade[i] = 'F';}

for (int i = 0; i < num; i++) {
cout<<"Student "<<i + 1<<" score is "<<score[i]<<" and grade is "<<grade[i]<<endl;
}

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:
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
// 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 <iostream>
#include <vector>

void waitForEnter();

int main()
{ 
    std::cout<<"Enter the number of students: ";
    int num = 0;
    std::cin >> num;

    std::cout << "Enter " << num << " scores: ";
    std::vector <int> 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
   char grade[] = { 'A', 'B', 'C', 'D', 'F' };
   int num;
   cout << "How many students? ";
   cin >> num;
   valarray<int> 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.

Please clarify.
Topic archived. No new replies allowed.