# Thread: Sorting three integers from the user in ascending order help

1. ## Sorting three integers from the user in ascending order help

Hello,
I'm trying to get a user to input 3 integers (including negatives) and then sort those three into ascending order. When i type in 4 -2 9 which is the example, i kept receiving -2 4 9 but no other examples would work. What's going on in my code? I tried something different for the second number being the largest. I think negative numbers are not being sorted correctly?

Code:
```#include <stdio.h>
#include<stdlib.h>
#define MAX 10

int main()
{

char a[MAX];
char b[MAX];
char c[MAX];
int number1, number2, number3;

printf("Enter 3 integers separated by a space: ");
fflush(stdout);
scanf("%s %s %s", a, b, c);

number1=atoi(a);
number2=atoi(b);
number3=atoi(c);

//Cases where first number is the biggest
if (number1 >> number2 >> number3)
{
printf("%d %d %d\n", number1, number2, number3);
}

if (number1 >> number2 >> number3)
{
printf("%d %d %d\n", number1, number2, number3);
}
if (number1 >> number3 >> number2)
{
printf("%d %d %d\n", number1, number3, number2);
}

//Cases where second number is the biggest
if ((number2 >> number1) & (number2 >> number3) & (number1 >> number3))
{
printf("%d %d %d\n", number2, number1, number3);
}

if ((number2 >> number3) & (number2 >> number1) & (number3 >> number1))
{
printf("%d %d %d\n", number2, number3, number1);
}

//Cases where third number is the biggest
if (number3 >> number1 >> number2)
{
printf("%d %d %d\n", number3, number1, number2);
}
if (number3 >> number2 >> number1)
{
printf("%d %d %d\n", number3, number2, number1);
}

return 0;
}```

2. >> does not do what you think it does.

3. What should i be using instead? I figured it out, i needed >= instead of >>!

4. Learn your operators. Also, even if you were using the right ones, multiple comparisons don't work like that in C. You need to compare two operands at a time. In your case a set if min/max functions would probably be best.

5. ## Utilized two >= statements with an AND

Code:
```#include <stdio.h>#include<stdlib.h>
#define MAX 10

int main()
{
char a[MAX], b[MAX], c[MAX];
int number1, number2, number3;
printf("Enter 3 integers separated by a space: ");
fflush(stdout);
scanf("%s %s %s", a, b, c);

number1=atoi(a);
number2=atoi(b);
number3=atoi(c);

//Cases where first number is the biggest
if ((number1 >= number2) & (number2 >= number3))
{
printf("%d %d %d\n", number3, number2, number1);
}

if ((number1 >= number3) & (number3 >= number2))
{
printf("%d %d %d\n", number2, number3, number1);
}

//Cases where second number is the biggest
if ((number2 >= number1) & (number1 >= number3))
{
printf("%d %d %d\n", number3, number1, number2);
}

if ((number2 >= number3) & (number3 >= number1))
{
printf("%d %d %d\n", number1, number3, number2);
}

//Cases where third number is the biggest
if ((number3 >= number1) & (number1 >= number2))
{
printf("%d %d %d\n", number2, number1, number3);
}
if ((number3 >= number2) & (number2 >= number1))
{
printf("%d %d %d\n", number1, number2, number3);
}

return 0;
}```

6. The & operator does a bitwise AND. Check out logic operators.

7. ## > vs >>

The & operator does a bitwise AND. Check out logic operators.
Noted and fixed, thanks for the response!

8. In addition to what others have advised, you are not using scanf() as it was designed.

scanf() will convert text data to an integer without having to use atoi().

Code:
```int main()

{

char a[MAX], b[MAX], c[MAX];
int number1, number2, number3;
printf("Enter 3 integers separated by a space: ");
fflush(stdout);

scanf("%s %s %s", a, b, c);

number1=atoi(a);

number2=atoi(b);

number3=atoi(c);

...
}```
try this:

Code:
```int main()
{

int number1 = 0;
int number2 = 0;
int number3 = 0;

printf("Enter 3 integers separated by a space: ");
fflush(stdout);
scanf("%d %d %d", &number1, &number2, &number3);
...
}```

Of course, you should add error checking to inure you hae received valid data from scanf(), no matter which method you use.

Also, you should initialize all your local variables!