Highest and lowest score, user input

Hi everyone! For an assignment I needed to collect student scores from user input. The amount of students would be up to the user so I used the exam_score[class_size] for the array. Everything works for far so I'm happy with it. I need to calculate the highest score entered and the lowest score entered. How do I do that? I'm completely stuck there.

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
{
	int class_size, High_Score, Low_Score, Class_Avg, score_sum=0; //declaring all integer values

	cout << "How many students are in your class? "; //ask user for class size
	cin >> class_size; //allow user to enter the class size
	cout << endl; //create a line space for next output

	int exam_score[class_size]; //create an array to store exam scores in. The size of the array will be the user-inputted class size

	for(int i=1; i<=class_size; i++) //for loop that will ask for score for as many students as the user inputted.
	{
	cout << "What is the final score for student " << i << ": "; //asks for score
	cin >> exam_score[i]; //allows user to enter scores
	score_sum=score_sum+exam_score[i]; //calculates the sum of all the entered scores
	}

	Class_Avg=score_sum/class_size; //calculates the average of the scores

	cout << endl; //creates line break
	cout << "Highest Score: \t\t" << High_Score << endl;
	cout << "Lowest Score: \t\t" << Low_Score << endl;
	cout << "Class Average: \t\t" << Class_Avg << endl; //outputs the average calculated above

	return 0;
}
Hello newprogrammergirl,

For some of this I will not repeat what I have said. http://www.cplusplus.com/forum/beginner/236550/#msg1058371

Line 8. This way of defining an array is not allowed most of the time. What you put between the []s needs to be a number like 10 or a variable defined as a constant with "constexpr" or "const". This has to be before compile time so the compiler knows how much memory to set aside for the array.

Line 14 can be shortened to: score_sum += exam_score[i];

Hope that helps,

Andy
That did help, so, thanks! How do I go about finding the highest and lowest exam scores? That's where I'm stuck.
There are two approaches. The first works whether you have values stored in array or not.

Look at: http://www.cplusplus.com/reference/limits/numeric_limits/

Would it make sense to start your code with:
1
2
int High_Score = std::numeric_limits<int>::min();
int Low_Score  = std::numeric_limits<int>::max();

Do you agree that no matter what input you will get, it cannot be lower than initial value of High_Score? Low_Score has similar situation.

Now, when you look at every new value, it might be higher than High_Score. In that case you should update the High_Score. Can you see why, after seeing all values, the High_Score will have expected value?

Similarly, when you look at every new value, it might be lower than Low_Score. In that case you should update the Low_Score.


The second approach is more suitable for existing array of values. There you know that you have at least one value. Therefore, you can initialize both High_Score and Low_Score with the first element of the array and then proceed with the rest of elements like in the first approach.

There is also http://www.cplusplus.com/reference/algorithm/minmax_element/
but you are supposed to think and practice, rather than resort to premade solutions (even though their proper use is a [different] challenge).
Topic archived. No new replies allowed.