So let me start off by saying that I saw the old archived post on this topic, but I found it not really helpful, and there was no way to add more to it. Using only if/else statements (no loops) I need to write a code that sorts any 4 integers in ascending order. I really don't want to do this with the brute force method, but a hint I have been given is to sort the first two numbers, and use some more if/else then put the third number in the right spot, and then some more if/else to put the fourth number in the right spot. Supposedly 6 if/else statements will do the job. Unfortunately this just confused me even more!
The following is the most coherent code that I have so far, and I think its starting to look like brute force! I don't think its right because I would need some more if/else relationships to fully describe the relationships besides the 6 main ones listed below. It's incomplete so don't try to run it!
Yep, we can't use that either. Nor min :). The only functions I can use are ones that I created myself...
I'll wiki bubble sort :)
Update: is there any sort of bubble sort available for if/else?
The only ones I've seen are for arrays and I can't use that either!
void swap(int& num1, int& num2)
{
int temp = num1;
num1 = num2;
num2 = num1;
}
int main()
{
int a, b, c, d;
cout << "Please input 4 integers: ";
cin >> a >> b >> c >> d;
if (b < a)
swap(a, b);
if (c < b)
swap(b, c);
if (b < a)
swap(a, b);
if (d < c)
swap(c, d);
if (c < b)
swap(b, c);
if (b < a)
swap(a, b);
cout << a << ' ' << b << ' ' << c << ' ' << d;
return 0;
}
Think of what would happen if the code only had a single one of those, and the input was 4 3 2 1.
Also, if you can only have the main function, then just move the code from the swap function into the main function. I only had it as a separate function so that the code would be shorter.