1. ## An unprecedented n00b thread...

Omg can someone please inform me on how I can sort 3 integers only with basic commands like printf, scanf, and if statements? No looping or switches.

I tried heaps of things, creating high and low integers but this is what I've got at the moment:

Code:
```#include <stdio.h>

int main (int argc, char *argv[]) {

int a, b, c;

scanf("%d, %d, %d", &a, &b, &c);

//Determine highest

if ( (a > b) && (a > c) && (b > c ) ) {                   /* for a, b, c */
printf(" Your numbers in order are: %d %d %d\n ", a, b, c);
}
if ( (b > a) && (b > c) && (c < a) ) {                  /* for b, a, c */
printf(" Your numbers in order are: %d %d %d\n ", b, a, c);
}
if ( (a > c) && (a > b) && (c > b) ) {
printf(" Your numbers in order are %d %d %d\n", a, c, b); /* for a, c, b */
}
if ( (b > a) && (b > c) && (c > a) ) {
printf("Your numbers in order are %d %d %d\n", b, c, a); /* for b, c, a */
}
if ( (c > a) && (c > b) && (a > b) ) {
printf("Your numbers in order are %d %d %d\n", c, a, b);  /* for c, a, b */
}
if ( (c > a) && (c > b) && (b > a) ) {
printf("Your numbers in order are %d %d %d\n", c, b, a);  /*  for c, b, a */
}

return 0;
}```

2. That is a bit excessively complicated. Assuming you don't actually need to keep a, b and c with their current values, you can use the "sorting approah":
if (b > a) swap a with b;
if (b > c) swap b with c;
if (a > c) swap a with c;
Now a > b > c

Swapping is done by storing the first value in a temporary variable, then storing the second in the first, finally storing temp back in the second one.

--
Mats

3. Won''t work.

Try 3 5 4.

If b > a, swap a with b: yields 5 3 4
If b > c, swap b with c: yields 5 3 4
If a > c, swap a with c: yields 4 3 5

Todd

4. Try this:

if b < a, swap a with b
if c < a, swap a with c
if c < b, swap b with c

Todd

5. Doh!

so:
if (a > c) swap(a, c);
if (b > c) swap(b, c);
if (a > b) swap(a, b);

I just tested that with varying inputs, and as far as I can tell, it works.

--
Mats

6. Since you're not allowed to use loops, I'd use a array -- move the #'s as required,

Run it n - 1 times. For example,
Code:
```void premote_three(int * numbers)
{
int temp = 0;

if(numbers[2] > numbers[1])
{
temp = numbers[1];
numbers[1] = numbers[2];
numbers[2] = temp;
}

if(numbers[1] > numbers[0])
{
temp = numbers[0];
numbers[0] = numbers[1];
numbers[1] = temp;
}
return;
}```
First run, on 3, 5, 4: 5, 3, 4
Second run (on 5, 3, 4): 5, 4, 3

Ugly but it works... of course it doesn't have to be an array (but if they then said -- "Do the same thing using loops", you'd already be ahead )

7. Thanks for the help. But I should have stressed more that I'm trying to just use If statements. To simplify it can I create a new integer or float or something?
The output I get with my original code that I posted seems to be two random large numbers and the value for 'a' in between.

8. Originally Posted by gman89
Thanks for the help. But I should have stressed more that I'm trying to just use If statements. To simplify it can I create a new integer or float or something?
The output I get with my original code that I posted seems to be two random large numbers and the value for 'a' in between.
You're probably not entering data as you've told scanf to expect: number comma space number comma space number.

Now, ask yourself what happens with your program when you enter: 3, 4, 3

Also, lets have a go at simplifying your code. First thing to notice is that the first 3 if statements all include a test for (a > c) or (c < a) which are the same thing. So those first three can be grouped inside an if statement that only does that check once:
Code:
```if (a > c) {
if ( (a > b) && (b > c) ) {                  /* for a, b, c */
printf("Your numbers in order are: &#37;d %d %d\n", a, b, c);
}
if ( (b > a) && (b > c) ) {                  /* for b, a, c */
printf("Your numbers in order are: %d %d %d\n", b, a, c);
}
if ( (a > b) && (c > b) ) {                  /* for a, c, b */
printf(" Your numbers in order are %d %d %d\n", a, c, b);
}
}```
That's step one. Now can you apply the same kind of simplification again inside that? What about applying the same kind of simplification to the other 3 existing if-statements too?

9. Hey thanks, that actually worked when I entered integers with comma's in between. I never would have picked that up.