User populated arrays

Hi, all. I'm working on another homework assignment, and once again, I'm having a bit of trouble. I've searched the web quite extensively and everywhere I look, the answer always seems to be to use a vector, however, my professor specifically instructed us to use an array. My assignment is as follows :


#7.12 (Assigning grades) Write a program that reads
student scores, gets the best score, and then assigns
grades based on the following scheme:
Grade is A if score is >= best – 10;
Grade is B if score is >= best – 20;
Grade is C if score is >= best – 30;
Grade is D if score is >= best – 40;
Grade is F otherwise.
The program prompts the user to enter the total number of
students, then prompts the user to enter all the scores,
and concludes by displaying the grades. Here is a sample
Run:
Enter the number of students: 4 [enter]
Enter 4 scores: 40 55 70 58 [enter]
Student 0 score is 40 grade is C
Student 1 score is 55 grade is B
Student 2 score is 70 grade is A
Student 3 score is 58 grade is B


I've created an array, with a size of 25 ( just a random number I've selected ) and I think I've successfully written the code to populate the array, and to determine the max, however the actual printing of the statements is where I'm stuck. No matter what, the outcome always seems to be 'D'. Any help is greatly appreciated !!


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

int main()

{
    char grade;
    int students, score;
    int studentSCORES[25];
    
    cout << "Enter the number of students: ";
    cin >> students;
    cout << "Enter " << students << " scores: ";
    for (int i = 0; i < students; i++)
    {
        cin >> score;
    }
    
    double max = studentSCORES[0];
    for (int j = 0; j < students; j++)
    {
        if (studentSCORES[j] > max) max = studentSCORES[j];
    }
    
    for (int k = 0; k < students; k++)
    {
        if (studentSCORES[k] >= max - 10)
            grade='A';
        
        else if (studentSCORES[k] >= max - 20)
            grade='B';
        
        else if (studentSCORES[k] >= max - 30)
            grade='C';
        
        else if (studentSCORES[k] >= max - 40)
            grade='D';
        
        else grade='F';
        
        for (int i=0; i <= students; i++)
        {
            cout << "Student " << i << " has score " << score << " and grade " << grade << ".";
        }
        
    return 0;
    }
}
Last edited on
There are some errors.
In such cases, we need arrays with dynamic memory allocation or vectors. Take a look here:

http://www.cplusplus.com/doc/tutorial/dynamic/

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

int main()

{
    int students;
    
    cout << "Enter the number of students: ";
    cin >> students;
    
    if(students <= 0) return 1;
    
    int *studentSCORES = new int[students]; // dynamic memory allocation of int
    char *grade = new char[students];		// the same for char
    
    cout << "Enter " << students << " scores: \n";
    for (int i = 0; i < students; i++)
    {
		cout << "student " << i << " score: ";
        cin >> studentSCORES[i];
    }
    
    double max = studentSCORES[0];
    for (int j = 0; j < students; j++)
        if (studentSCORES[j] > max) max = studentSCORES[j];
    
    for (int k = 0; k < students; k++)
    {
        if (studentSCORES[k] >= max - 10)
            grade[k] = 'A';
        
        else if (studentSCORES[k] >= max - 20)
            grade[k] = 'B';
        
        else if (studentSCORES[k] >= max - 30)
            grade[k] = 'C';
        
        else if (studentSCORES[k] >= max - 40)
            grade[k] = 'D';
        
        else grade[k] = 'F';
    }
        cout << '\n';
        for (int i = 0; i < students; i++)
            cout << "Student " << i << " has score " << 
            studentSCORES[i] << " and grade " << grade[i] << ".\n";
            
    delete[] studentSCORES;	// release memory allocation
    delete[] grade;
    return 0;
}
Enter the number of students: 4
Enter 4 scores: 
student 0 score: 40
student 1 score: 55
student 2 score: 70
student 3 score: 58

Student 0 has score 40 and grade C.
Student 1 has score 55 and grade B.
Student 2 has score 70 and grade A.
Student 3 has score 58 and grade B.
Last edited on
Thank you so much !!!!!
You're welcome! Please mark this thread as Solved.
Topic archived. No new replies allowed.