C++: How to find a the Min. & Max. number from a set of numbers

Problem Solved.
Last edited on
You'll have to declare two new variables when testing for the min and max numbers.

Let's say you have array my_nums[10] which holds 10 numbers of type int

You can assign the value of my_nums[0] to both min and max, then interate through the array checking if the current index is lower(min) or higher(max) then the current min and max, if either evaluates to true, set min and or max to that array index value.

Last edited on
The main loop will look the following way

1
2
3
4
5
6
7
8
9
10
11
for ( int i = 0; i < Num_Input; i++ )
{
   cout << "Enter a number  here: " ;
   cin >> Stored_Num;
   if ( i == 0 ) min = max = Stored_Num;
   else 
   {
      if ( Stored_Num < min ) min = Stored_Num;
      if ( max < Stored_Num ) max = Stored_Num;
   }
}
Last edited on
Topic archived. No new replies allowed.