samedi 10 octobre 2015

Consecutive comparisons using if statements inside a while loop

I am supposed to compare two consecutive integers, i and j, that are given from a list of integers separated by whitespace which end with a 0 and, if i is less than j, I compare j to max and i to min. If the opposite, I compare j to min and i to max. The output is supposed to be each comparison I do with max, min, i, and j. Additionally, the list must be greater than 2 integers. If it is less then I am supposed to output 0. However the program does not seem to execute the if statements correctly.

int i = 1;
int j;
int max = 0;
int min = 0;
int counter = 0;

while (i != 0) {
    cin >> i;

    if (counter == 0) {
        cout << 0 << endl;
        i = min;
        j = max;
    } else if (counter == 1) {
        cout << 0 << endl;
        i = min;
        j = max;
    } else {
        if (i < j) {
            if (j > max) {
                cout << j << " " << max << endl;
                max = j;
            } 
            if (i < min) {
                cout << i << " " << min << endl;
                min = i;
            }
        } 
        else {
            if (j < min) {
                cout << j << " " << min << endl;
                min = j;
            }
            if (i > max) {
                cout << i << " " << max << endl;
                max = i;
            }
        }
    }
    j = i;
    counter += 1;
}

}

Aucun commentaire:

Enregistrer un commentaire