I wrote this code to sort 3 integers, but I can't figure out how to add the 4th integer.

#include <iostream>
using namespace std;
int main ()
{
    cout << "Enter 4 integers: ";
    int a, b, c, d;

    cin >> a >> b >> c >> d;

    if (a<b)
    {
        if (b<c)
        {
            cout << a << b << c << endl;
        }
        else if (a<c)
        {
            cout << a << c << b << endl;
        }
        else cout << c << a << b << endl;
    }

    else if (b<c)
    {
        if (c<a)
        {
            cout << b << c << a << endl;
        }
        else if (b<a)
        {
            cout << b << a << c << endl;
        }
        else cout << a << b << c << endl;
    }

    else if (c<a)
    {
        if (a<b)
        {
            cout << c << a << b << endl;
        }
        else if (c<b)
        {
            cout << c << b << a << endl;
        }
        else cout << b << c << a << endl;
    }

    else 
    {
        cout << a << b << c << d;
    }
    return 0;
}

For this assignment we are supposed to sort 4 numbers, only using if-else statements, and only using methods we explicitely learned in class.

I couldn't figure out a better way to do it, so I ended up doing it like this. It does what I need it to do, but it's kind of long
and messy. It's already turned it, but I want to know how to make it better. In the instructions for this problem my professor wrote:
"One way might be to sort the first 2 numbers, then to use a series of if-else statements to add in the third number in the correct
spot, then another series of if-else statements to add in the fourth number in the correct spot (You would need 6 total if-else
statements with this method)." Nothing that I tried came out to only 6 if-else statements. How would I go about that?

#include <iostream>
using namespace std;

int main ()
{
    cout << "Enter 4 integers: ";
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    if (a<b && b<c && c<d)
    {
        cout << a << " " << b << " " << c << " " << d << endl;
    }
    else if (a<b && b<d && d<c)
    {
        cout << a << " " << b << " " << d << " " << c << endl;
    }
    else if (a<c && c<b && b<d)
    {
        cout << a << " " << c << " " << b << " " << d << endl;
    }
    else if (a<c && c<d && d<b)
    {
        cout << a << " " << c << " " << d << " " << b << endl;
    }
    else if (a<d && d<b && b<c)
    {
        cout << a << " " << d << " " << b << " " << c << endl;
    }
    else if (a<d && d<c && c<b)
    {
        cout << a << " " << d << " " << c << " " << b << endl;
    }
    else if (b<a && a<c && c<d)
    {
        cout << b << " " << a << " " << c << " " << d << endl;
    }
    else if (b<a && a<d && d<c)
    {
        cout << b << " " << a << " " << d << " " << c << endl;
    }
    else if (b<c && c<d && d<a)
    {
        cout << b << " " << c << " " << d << " " << a << endl;
    }
    else if (b<c && c<a && a<d)
    {
        cout << b << " " << c << " " << a << " " << d << endl;
    }
    else if (b<d && d<a && a<c)
    {
        cout << b << " " << d << " " << a << " " << c << endl;
    }
    else if (b<d && d<c && c<a)
    {
        cout << b << " " << d << " " << c << " " << a << endl;
    }
    else if (c<a && a<b && b<d)
    {
        cout << c << " " << a << " " << b << " " << d << endl;
    }
    else if (c<a && a<d && d<b)
    {
        cout << c << " " << a << " " << d << " " << b << endl;
    }
    else if (c<b && b<a && a<d)
    {
        cout << c << " " << b << " " << a << " " << d << endl;
    }
    else if (c<b && b<d && d<a)
    {
        cout << c << " " << b << " " << d << " " << a << endl;
    }
    else if (c<d && d<b && b<a)
    {
        cout << c << " " << d << " " << b << " " << a << endl;
    }
    else if (c<d && d<a && a<b)
    { 
        cout << c << " " << d << " " << a << " " << b << endl;
    }
    else if (d<a && a<b && b<c)
    {
        cout << d << " " << a << " " << b << " " << c << endl;
    }
    else if (d<a && a<c && c<b)
    {
        cout << d << " " << a << " " << c << " " << b << endl;
    }
    else if (d<b && b<a && a<c)
    {
        cout << d << " " << b << " " << a << " " << c << endl;
    }
    else if (d<b && b<c && c<a)
    {
        cout << d << " " << b << " " << c << " " << a << endl;
    }
    else if (d<c && c<b && b<a)
    {
        cout << d << " " << c << " " << b << " " << a << endl;
    }
    else if (d<c && c<a && a<b)
    {
        cout << d << " " << c << " " << a << " " << b << endl;
    }
    return 0;
}

First, you need to know that you are allowed to swap the values of variables. In other words, the variable a,b,c,d don't have to have the same value all throughout your program. So, the idea is not to print out the four variables in order, but to rearrange the values such that a < b < c < d at the end.

As an example, here is a start, the first if statement should be:

if( b < a ) {
  int tmp = a;
  a = b;
  b = tmp;     // at this point a stores the original value of b, and vice versa, the values are swapped.
};

After that if-statement, the first two values will be in order.

Then, you need an if-else statement to find out where c should go. If c should go at the start, then you need to "rotate" the values (a kind of double swap). If c should go between a and b, then you need to swap b and c. And if c should go after both a and b, then you don't need to do anything. This amounts to one if and one else-if statement.

Finally, you do the same for d. This time you'll one if and two else-if statements, which makes a total of 6 if-statements, as hinted to you.

It is also possible to proceed by putting c and d in correct order also (as with a and b), and then proceed to interleave the (a,b) and (c,d) sequences. That procedure is called merge-sort (while the above procedure is called insertion-sort).

mike's code comes must cleaner, if you do swap by function and do function find_index_for to get the place where to insert and and insert_to_index to insert the value there, if you are allowed to use functions and sequence type likea array or std::vector (which probably would not be case, but I want to still mention this)

For me best comparisions would be to put first two in order and second two in order (same operation, different place). So actually you could have function put_in_order which swaps the variables if they are not in order, otherwise does nothing (call by reference). Then we can compare minimums 1st and 3rd values, and maximums 2nd and 4th value and do same put_in_order. Now we have minimum first and maximum last. It remains only to compare and swap if necessary the middle 2nd and 3rd values.

If you are not allowed functions, you could do neat preprocessor input with macro for put_in_order and submit the result after preprocessing stage ;)

This article has been dead for over six months. Start a new discussion instead.