# Thread: 3 integers in ascending order

1. ## 3 integers in ascending order

hi

this is my 1st post here, hope i'll get know learn many things.

I am just starting an introductory course on C programming in ANSI in my university.

the other day my sir gave me an assignment telling us to prompt the user for 3 integers and the output should give them in ascending order.

I tried my best....and this is how it looks.

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

int a,b,c;

void main (){

scanf("%d %d %d",  &a,&b,&c);
printf("The integers from smallest to largest are:\n");

if (a>b) {
if (b<c){
printf("%d %d %d", a,b,c);
}

else if (a<c){
printf("%d %d %d", a,c,b);
}
else {
printf("%d %d %d", c,a,b);
}

else{
if (b>c){
printf("%d %d %d", c,b,a);
}
else if (b<c){
printf("%d %d %d", b,c,a);
}
else {
printf("%d %d %d", b,a,c);
}
}
}
}```
can some one please find me the flaw??? it doesnt run

Hobilla.. Thanks

2. Your first if statement says "if (a>b)" but in each of the statements where that's true, you print a before b.

Also, there's no such thing as "void main" and there's probably no good reason for the variables a, b, and c to live "outside" your program.

3. ok i changed the a>b to a<b...then its ok right?

what do i do with void main?

by outside, what do you mean?

should i place the integers after the main function line??

how do i get it to run??

4. Originally Posted by hobilla
what do i do with void main?
Change it to int main. You should also return 0 at the end of the main function (though depending on your exact compiler settings, that may be optional for the main function).

Originally Posted by hobilla
by outside, what do you mean?

should i place the integers after the main function line??
Yes, declaring those variables at the start of the main function would be appropriate.

Originally Posted by hobilla
how do i get it to run??
To be more precise: are you trying to get it to compile and run, or does it already compile but gives incorrect output? In both cases you should tell us how does it not work (though you have already been told a possible solution).

5. You haven't closed off your first if loop properly, you have it closed off after the second else statement. Which not only makes two else statements inside that if loop (think that creates a compiler error?), but it will not print anything if a is less than b.

6. Hi hobilla,

your code is broken by design. No human would try to sort a set of three elements in this way, it's too complicated. Consider deleting all your code and re-start from scratch. Here are a few hints:

1)
You only need three comparisons to compare every element of the set with every other element (due to transitivity of "<="), e.g. "a<=b", "b<=c" and "c<=a" will do the trick.

2)
Without thinking about the actual implementation in C, consider the following situation: you have three balls, each of which has a different size. How would you sort them in ascending order? Hint: start with the two outmost balls.

And more generally, here are some more tips:

1) Don't nest if-statements. You're trying to put too much information in one sentence. Keep it simple.
2) Use only one printf(). That's what you'd do in real life, too.

Greets,
Philip

7. thank u guys
@laserlight: it duznt compile, has errors...And also, can u tel me the structure of the if else thingy...im really confused with them....nested if else?? else if ladder?? all these...:S

@snafuist: thanks for your info. I already thought of it like that. But sorry, im relly weak in algorithms. I just cannot even figure out the 2 balls problem u wrote.
can u please guide me with this..??

8. Originally Posted by hobilla
it duznt compile, has errors.
You should post the updated code and the exact error messages.

Originally Posted by hobilla
can u please guide me with this..?
Think about swapping balls if they are not in the correct order.