highest to lowest

im new to c++ and i am having a bit of trouble in finding lowest value. the lowest value is always returning 1 or -1 sometimes. i appreciate any help, thank you.

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
 #include<iostream>

using namespace std;

int main(){
	
	int scores[5];
	int highest,lowest;
		
highest=scores[0];
lowest=scores[0];

	for(int x = 0; x<5; x++){
		cout<<"Enter score for student " <<x+1 <<" : ";
		cin>>scores[x];
		
			if(highest<scores[x]){
				highest=scores[x];
			}
		
			else if(lowest>scores[x]){
			lowest=scores[x];
			}
	
	}

	cout<<"Highest score is: " <<highest <<endl;
	cout<<"Lowest score is: " <<lowest <<endl;
	
}
What are the values of 'highest' and 'lowest' on line 12?

(Remember that you will compare to those values after the user has given the first score.)


For an alternative, see: http://www.cplusplus.com/reference/limits/numeric_limits/
closed account (E0p9LyTq)
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
#include <iostream>
#include <limits>

int main()
{
   const int maxScores = 5;
   int scores[maxScores];

   // get a number lower than the lowest possible input
   int highest = std::numeric_limits<int>::min();
   
   // get a number higher than the highest possible input
   int lowest = std::numeric_limits<int>::max();

   //highest = scores[0]; // the scores array filled with garbage values
   //lowest = scores[0];

   for (int x = 0; x < maxScores; x++)
   {
      std::cout << "Enter score for student " << x + 1 << ": ";
      std::cin >> scores[x];

      if (highest < scores[x])
      {
         highest = scores[x];
      }

      if (lowest > scores[x])
      {
         lowest = scores[x];
      }
   }

   std::cout << "Highest score is: " << highest << '\n';
   std::cout << "Lowest score is: " << lowest << '\n';
}

Enter score for student 1: 5
Enter score for student 2: 25
Enter score for student 3: 18
Enter score for student 4: 4
Enter score for student 5: 6
Highest score is: 25
Lowest score is: 4
Topic archived. No new replies allowed.