dimanche 24 janvier 2021

Ordering Inputs Using Only If-Else Statements

I'm trying to take five inputs from a user, then trying to sort those inputs using five separate variables. The obvious answers to a question like this might be to just use a pre-written function that does the sorting and to use structures like an array or a vector, but that's doing things the easy way -- it would normally be fine, but I'm trying to do things the hard way so that I can figure out what kind of magic is going on under the hood and whether or not I understand the basic logic.

I have the following rat's nest of an if-else statement to determine the position of each input in the sequence (my apologies in advance):

    if (i <= i2) {                   //checking against every other value to determine position
        if (i <= i3) {
            if (i <= i4) {
                if (i <= i5) {
                    first = i;
                }
                else if (i >= i2) {
                    if (i <= i3) {
                        if (i <= i4) {
                            if (i <= i5) {
                                second = i;
                            }
                            else if (i >= i2) {
                                if (i >= i3) {
                                    if (i <= i4) {
                                        if (i <= i5) {
                                            third = i;
                                        }
                                        else if (i >= i2) {
                                            if (i >= i3) {
                                                if (i >= i4) {
                                                    if (i <= i5) {
                                                        fourth = i;
                                                    }
                                                    else if (i >= i2) {
                                                        if (i >= i3) {
                                                            if (i >= i4) {
                                                                if (i >= i5) {
                                                                    fifth = i;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }                                          //followed by four more of these with the variables shifted around


And it's the same for each variable, five times over, but with the appropriate variables in their corresponding places, such that i, i2, i3, i4, and i5 are all cross-referenced with one another to determine their place.

The main problem that I'm encountering right now is that this nested statement doesn't seem to be working with the placement variables (first, second, third, etc.).

This output

//output smallest to largest
    std::cout << first << ", " << second << ", " << third << ", " << fourth << ", " << fifth << '\n';

is not generating what I think it should be generating. If I input any numbers, say 1 thru 5 I'm only ever getting 1, 0, 0, 0, 0 back from the console. If I understand correctly, then it means that the latter four variables (which are initialized at zero) are not being assigned a new value, but I cannot figure out why that would be unless the if nests are being skipped over. How might I correct this for the proper outputs without using any of the extra bells and whistles from other libraries or writing a new function to handle it?

Aucun commentaire:

Enregistrer un commentaire