# Thread: Problem with ascending order Program

1. ## Problem with ascending order Program

Alright, I have written a program for class that allows the user to input three integers then sorts then and prints them in ascending order.

It works sometimes but sometimes it prints strange results. I'll post the code below and then beneath the code I'll give you a few test results. Any help would be greatly appreciated.

Code:
```// This program takes integers from user input and
// outputs them in ascending order.

#include <stdio.h>

main (void)
{

int a, b, c, min, mid, max;

printf("Enter Number 1: ");
scanf("%d", &a);
printf("Enter Number 2: ");
scanf("%d", &b);
printf("Enter Number 3: ");
scanf("%d", &c);

if (a > b) {
if (a > c) {
max = a;
if (b > c){
mid = b;
min = c;
}
else
mid = c;
min = b;
}
else
max = c;
mid = a;
min = b;

}
else if (c > b) {
max = c;
mid = b;
min = a;
}
else if (b > c) {
max = b;
if (a > c) {
mid = a;
min = c;
}
else
mid = c;
min = a;
}

printf("%d \n%d \n%d \n", min, mid, max);

return 0;
}```
When I input 1, 2, 3... it prints out the correct order.

When I input 3, 2, 1... it prints out 2, 3, 3.

When I input -1, -2, -3 it prints out -2, -1, -1

When I input -3, -2, -1 it prints out -3, -2, -1

When I input -3, -2, -4 it prints out -3, -3, -2

It seems that when a < b the results are wrong..

Does anyone have any ideas? I'm sure its obvious to a fresh pair of eyes, I've just been looking at it for hours and my brain is fried

2. the code looks pretty long and, although i havent taken a very good look at it, perhaps you could draw out on paper a method of finding the min, middle, and max of 3 numbers and discover a more efficient method. when doing so, think not in terms of programming, just simple english/math/logic, then convert your algorithm to code.

an important thing to point out that i notice is your 'else' blocks. in each of your 3 'else' blocks you have more than one statement, however, only the first of the n statements belongs to the actual 'else' block. for example, if your 'if' clause gets executed, it will then execute the statement after the first statement after the 'else'.
what im saying may not be clear.. the point is, just like your 'if' statements, you need to put {}s around your else block when it has more than one statement to be executed.

for example:
Code:
```if(...)
{
;//...
}
else
mid = c;
min = a;```
if the 'if' condition is false, it will of course go to the 'else' clause. it will then execute the statement 'mid = c';. then it will execute the 'min = a'; statement, which seems fine. however, if the 'if' condition is true, it will, again, of course, execute whatever its supposed to in the if block. after those statements, it will also execute 'min = a';. so in both cases 'min = a'; is executed, so you should probably put {} around the statements in your else clauses' if you want them to be executed only 'else'. im sure this will be part of your problem.

hope it makes sense.

3. You could also try using a sorting function like bubble or selection sort. They are bit more complex to code but they do also reduce the code lines you have to write as they are small compact functions. Oh and there is also quicksort

4. try using && "and", or || "or" operators...

5. using the && method worked, thanks for your suggestion, greatly appreciated.