# Trying to sort numbers from lowest to highest

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-19-2008
jw232
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.
• 01-19-2008
Daved
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.
• 01-19-2008
laserlight
Quote:

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.
• 01-19-2008
tabstop
Somebody just asked this problem, although they were looking specifically for the middle value. I believe a board search is in order.
• 01-19-2008
foxman
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 */```
• 01-19-2008
laserlight
foxman's example looks correct, though std::swap() would actually make it work :p
• 01-19-2008
jojo13
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.
• 01-19-2008
Elysia
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()`
• 01-19-2008
Soulrivers
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.
• 01-19-2008
dwks
Eh, std::sort() would be best of all, but that's just cheating. :)
• 01-19-2008
laserlight
Quote:

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).
• 01-20-2008
Soulrivers
Quote:

Originally Posted by laserlight
There is no such function for the standard vector container ;)

Yeah yeah, I meant algorithm. :P
• 01-20-2008
cpjust
Quote:

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 "\".
• 01-21-2008
Elysia
Using absolute paths is not usually the best solution. It's better to use relative paths or <>.
• 01-21-2008
jw232
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last