finding max. and min. values

i want to finding maximum and minimum values of 10 numbers using for loop and the program work wrong !! any help?!
#include <iostream>
using namespace std;

int main() {
int x;

int max = -999999999;
int min= 999999999;

for(int i=0;i<10;i++){
cin>>x;

if (x > max)
max = x;
else if(x<min)
min = x;
}

cout<<"minimum is "<<min<<endl;
cout << "Maximum is " << max << endl;

system("pause>>null");
return 0;
}
What the problem is? Program is working fine for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>
using namespace std;

int main() {
    int x;

    int max = std::numeric_limits<int>::min() ;
    int min = std::numeric_limits<int>::max() ;

    for( int i=0; i<10; i++)
    {
        cin >> x;

        if (x > max)
            max = x;

        if( x < min )
            min = x;
    }

    cout<<"minimum is "<< min << endl;
    cout << "Maximum is " << max << endl;
}


Note that there is no else in the loop. It is possible for a value to be both greater than max and less than min. For instance, if you enter one value: 1. On the only iteration of the loop, both max and min should be set to that value.
Topic archived. No new replies allowed.