The question I am about to ask is pretty simple. It's part of a drill exercise I'm going through. I need to prompt the user to input 3 Integers, then output them in ascending order. This sounds like a very common question, but I would like to ask if it's possible ( and efficiently so )to code this program entirely using IF STATEMENTS. No loops, no arrays.
Does it require nesting IF STATEMENTS? More than 3 statements? How can you keep it to a reasonable size. In my first attempt, I've been doing impractical comparisons between A and B, B and C. Am I on the right track?
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "Enter Values to Order" << endl;
cin >> a >> b >> c;
if ( a >= b)
if( b>= c)
cout << c << " " << b << " " << a << endl;
else
cout << b << " " << c << " " << a << endl;
elseif (a >= c)
cout << c << " " << a << " " << b << endl;
else
cout << a << " " << c << " " << b << endl;
return 0;
I get wrong outputs when I use negative values, 0, and -1 though.
It can be done quite easily. There is something called "loop unrolling". In this case you know exactly how many iterations of the loop there are, you could just write the loop code three times (though adjusted to use your three variables instead of array indices)