Thread: Trying to sort numbers from lowest to highest

1. Trying to sort numbers from lowest to highest

Well I'm trying to sort three integers from lowest to highest without using loops, nested if-statements, multiple statements in the if-statements, or else statements. Is this possible?

This is what I've come up with so far:
Code:
```//Trying to sort numbers from lowest to highest

#include "C:\Visual C++ Projects\std_facilities_lib.h"

void main()
{
int i1; int i2; int i3;
int low; int med; int high;
cout << "Enter three integers separated by spaces:\n";
cin >> i1 >> i2 >> i3;
if (i1<i2)
if (i1<i3)
low=i1;
if (i2<i1)
if (i2<i3)
low=i2;
if (i3<i1)
if (i3<i2)
low=i3;

keep_window_open();
}```
It only finds the lowest number. I don't know how to find the lower number of the next two.

2. First, that won't work. As mentioned before, those are nested if statements, which aren't allowed. I would imagine this would take lots of if statements given those restrictions.

I would approach this problem by assigning low to the first number. Then I'd compare that to the second number and update the low if necessary. Then do the same with the third number.

You can follow the same steps for the high. For the medium, I guess you can do something similar to figure out which of the three numbers has not been used.

3. Well I'm trying to sort three integers from lowest to highest without using loops, nested if-statements, multiple statements in the if-statements, or else statements. Is this possible?
Yes.

4. Somebody just asked this problem, although they were looking specifically for the middle value. I believe a board search is in order.

5. Well, you could sort them, that's an easy way to know which is the lowest, highest and which is in middle

Code:
```if (i2 > i3)
// swap i2 and i3
if (i1 > i2)
// swap i1 and i2
if (i2 > i3)
// swap i2 and i3
/* A: i1 is lowest number, i2 is middle, i3 is highest */```

6. foxman's example looks correct, though std::swap() would actually make it work

7. I am not sure about your question. did you try the conditional operator?
something like:

if (i1<i2)
{
low =(i1<i3 ? i1 : i3);
med = (i2<i3 ? i2 : i3);
high =(i2<i3 ? i3 : i2);
}
if (i2<i1)
{
low =(i2<i3 ? i2 : i3);
med = (i1<i3 ? i1 : i3);
high =(i1<i3 ? i3 : i1);
}

but the problem is it is multiple statements.

8. But really. Think how you would do this in the real world. If you were given 3 numbers and had to sort them and put them in their proper place, how would you do it?
Code:
`void main()`

9. I'm going to assume that you either haven't learned about vectors yet, or that you are trying to figure this out without using vectors. Anyway, using the vector function sort() would be a better alternative in this case.

10. Eh, std::sort() would be best of all, but that's just cheating.

11. Anyway, using the vector function sort() would be a better alternative in this case.
There is no such function for the standard vector container
Besides, this appears to be either homework or a friendly challenge from some book/website, so resorting to a sort function (pun intended) from an existing library would be against the spirit of the problem (and possibly overkill for 3 items).

12. Originally Posted by laserlight
There is no such function for the standard vector container
Yeah yeah, I meant algorithm. :P

13. Originally Posted by jw232
This is what I've come up with so far:
Code:
```//Trying to sort numbers from lowest to highest

#include "C:\Visual C++ Projects\std_facilities_lib.h"

void main()
{
int i1; int i2; int i3;
int low; int med; int high;
cout << "Enter three integers separated by spaces:\n";
cin >> i1 >> i2 >> i3;
if (i1<i2)
if (i1<i3)
low=i1;
if (i2<i1)
if (i2<i3)
low=i2;
if (i3<i1)
if (i3<i2)
low=i3;

keep_window_open();
}```
BTW, you do know that backslashes need to be escaped if you want literal backslashes to appear in your string. i.e. use "\\" instead of "\".

14. Using absolute paths is not usually the best solution. It's better to use relative paths or <>.

15. Originally Posted by cpjust
BTW, you do know that backslashes need to be escaped if you want literal backslashes to appear in your string. i.e. use "\\" instead of "\".
Hmm it still worked anyways.