Best value in array

quick question, how do i get the best score, for example, after inputting 5 scores of 20, 40, 60, 80, 100, it tells me the best score is 100, before calculation of grades

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

int main()
{
    
const int Students = 100;

int scores[Students];
int NoOfScores;
int best = 100;
char grade;


cout << "Enter number of scores: ";

cin >> NoOfScores;

cout << "Enter " << NoOfScores << " scores: ";

    for (int i = 0; i < NoOfScores; i++)
    {
    
    cin >> scores[i];
    
        if (scores[i] > best)
        {
            
        best = scores[i];
        
        }    
    }

    for (int i = 0; i < NoOfScores; i++)
    
    {
    
    if (scores[i] >= best - 10)
    grade = 'A';
    
    else if (scores[i] >= best - 20)
    grade = 'B';
    
    else if (scores[i] >= best - 30)
    grade = 'C';
    
    else if (scores[i] >= best - 40)
    grade = 'C';
    
    else grade = 'F';
    
    cout << "Student " << (i+1) << " score is " << scores[i] << " and grade is " << grade << "\n";
    
    }

    return 0;
    
}
In your loop from 21 to 32, you're comparing each score so far to see if it's greater than what is currently the highest score. But you set the highest score to already be 100 from the start, so the lowest that "best" can possibly be is 100. Perhaps you want to initially set "best" to be a very low number, like -1.

This will give:
Enter number of scores: 3
Enter 3 scores: 40 60 80
Student 1 score is 40 and grade is C
Student 2 score is 60 and grade is B
Student 3 score is 80 and grade is A
Last edited on
but if i set to -1 the grades will not calculate properly, can you help me fix it? and also help me calculate largest grade input
Last edited on
if best is at 100 grades show properly i just want to find highest inputed value from the scores.
I don't know what it means to correctly calculate in this context, perhaps you should be more precise.

i just want to find highest inputed value from the scores.
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
#include <iostream>
using namespace std;

int main()
{
    const int MaxStudents = 100;
    
    int scores[MaxStudents];
    int NoOfScores;
    int best = -1;

    cout << "Enter number of scores: ";
    
    cin >> NoOfScores;
    
    cout << "Enter " << NoOfScores << " scores: ";

    for (int i = 0; i < NoOfScores; i++)
    { 
        cin >> scores[i];
    
        if (scores[i] > best)
        {
            
            best = scores[i];
        
        }    
    }
    
    cout << "best = " << best << '\n';
}

Enter number of scores: 3
Enter 3 scores: 60 80 70
best = 80
Last edited on
ok so the output i have right now is:

1
2
3
4
5
6
7
Enter number of scores: 5
Enter 5 scores: 20 30 40 50 60
Student 1 score is 20 and grade is F
Student 2 score is 30 and grade is F
Student 3 score is 40 and grade is F
Student 4 score is 50 and grade is F
Student 5 score is 60 and grade is C


the output i want is:

1
2
3
4
5
6
7
8
Enter number of scores: 5
Enter 5 scores: 20 30 40 50 60
The highest score is 60.
Student 1 score is 20 and grade is F
Student 2 score is 30 and grade is F
Student 3 score is 40 and grade is F
Student 4 score is 50 and grade is F
Student 5 score is 60 and grade is C
Last edited on
Perhaps "best" and "best" are separate things.

How about const int max = 100; and compute letter grades with max?
For the top student, have that int best = -1; and that best you update as before.


PS. What grade does one get with score of over 9000?


EDIT: If you want to print something, then print it.
E.g. cout << "best = " << best << '\n';
Last edited on
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
58
59
60
61
62
63
64

#include <iostream>
using namespace std;

    
const int Students = 100;

int scores[Students];
int NoOfScores;
int best = -1;
const int max = 100;
char grade;


int main()
{

cout << "Enter number of scores: ";

cin >> NoOfScores;

   
    cout << "Enter " << NoOfScores << " scores: ";

    for (int i = 0; i < NoOfScores; i++)
    { 
        cin >> scores[i];
    
        if (scores[i] > best)
        {
            
            best = scores[i];
        
        }    
    }
    
    cout << "The highest grade is " << best << ".\n";

    for (int i = 0; i < NoOfScores; i++)
    
    {
    
    if (scores[i] >= max - 10)
    grade = 'A';
    
    else if (scores[i] >= max - 20)
    grade = 'B';
    
    else if (scores[i] >= max - 30)
    grade = 'C';
    
    else if (scores[i] >= max - 40)
    grade = 'C';
    
    else grade = 'F';
    
    cout << "Student " << (i+1) << " score is " << scores[i] << " and grade is " << grade << "\n";
    
    }

    return 0;
    
}


Isn't working
@keskiverto
This is where "using namespace std;" can cause errors...

Rename your "max" variable to "max_score" or something like that.
(Or declare the your max variable inside the main function, instead of as a global variable.)

In general: Don't create global variables.
Last edited on
ok i got it now thanks alot
@Ganado has a very valid point here, a name like "max" is a bit obvious as a member of the std namespace.

However, there are times, maybe not with "max" specifically, but with other collisions which prompt that philosophy against "using namespace std;"

However, this code demonstrates a second point, and that is of not using a namespace.

If this code were in it's own namespace (which would mean this is not int main, typical of student programs), then "max" would have been inside a namespace, avoiding collision with a "using namespace std" (also, better if inside the user's namespace), such that the name isn't just max, but mns::max ( mns = my namespace), which differentiates the two from each other.

This goes to that general notion that somehow "using namespace std" is to be avoided. Many refuse, and with their own reasonable objections.

Again, though, "max" just isn't a good candidate for creating a collision with std, and there are other means of bringing the convenience of not being required to prepend "std::" in front of everything.

Ha, I didn't intend to bleed the other thread's discussion into this one. You're correct of course, there other means without having to prepend std:: onto everything.

Making the variable not global also solves it because it allows the compiler to unambiguously shadow the global identifier.

At least it isn't a #macro *cough* <windows.h> *cough*
Last edited on
Isn't working

That is not a sufficient description of a problem.

In this case the issue is that the compiler cannot compile the code, because:
 In function 'int main()':
43:22: error: reference to 'max' is ambiguous

The compiler sees more than one name 'max' and cannot decide which to use. They are:
11:11: note: candidates are:
const int max
template<class _Tp, class _Compare> const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
template<class _Tp> const _Tp& std::max(const _Tp&, const _Tp&)

In reality the compiler tends to be more verbose than that. The key point is to read what the compiler says and learn to understand the sometimes cryptic messages.

When reporting an issue it is important to show the verbatim error message. That helps others to focus on the issue. At the same time you learn to interpret the messages that your compiler writes.


Note that it is possible to explicitly refer to name that is in the global scope with prefix '::'.
if (scores[i] >= ::max - 10) // ok


PS. What is the "std::max" that jumped on us? It's "best of two":
http://www.cplusplus.com/reference/algorithm/max/

There is also "best of array": http://www.cplusplus.com/reference/algorithm/max_element/
(That returns position of element that has "best" value.)
Topic archived. No new replies allowed.