Question : How to find max/min integers in a vector?

If a user inputs 10 integers how do I find the max and the min of those integers? Not sure whether to use a while loop or an if loop. My code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
   vector<int> user_num(10);
   cout << "Enter 10 integers: ";
   for (int i = 1; i <= 10; i++)
   {
       cin >> user_num[i];
   }
   
   int largest = user_num[i];
   cout << "Max: ";
   for (int i = 1; i <= 10); i++)
   {
       if ( )
   }
    return 0;
}

Answer : How to find max/min integers in a vector?

Basically you'd use two variables representingthe current maximum and minimum, initialize them appropriately and compare each vectort element to them, e.g.

#include <limits.h>

int min = INT_MAX;
int max = INT_MIN;

// ...

Now, if your current user_num[i] is larger than the current 'max', replace 'max' with 'user_num[i]' and use the same method for 'min'.
Random Solutions  
 
programming4us programming4us