# Thread: 3 integers in ascending order-help

1. ## 3 integers in ascending order-help

For the life of me I can not figure out how to efficiently code placing three integers in ascending order! I can do it with a bazillion lines of code but I'm certain there is a shorter way.

int num1;
int num2;
int num3;

int min;
int mid;
int max;

(I'm too embarrassed to write those bazillion lines of code!)

any help would be greatly appreciated.

Alli

2. what do you mean?

something like this will work

int num1,num2,num3;

or

int min,mid,max;

3. whoops! I meant to say that the 3 integers are read in from a file ... then need to be sorted into ascending order then displayed on the screen. I just don't know how to write the tests using say five lines of code.

4. Well this is the algorithm - all you need to do is arrange the input, and figure out how to swap two variables

Code:
```    if ( a > b ) swap(a,b);
if ( b > c ) swap(b,c);   // c is now the largest
if ( a > b ) swap(a,b);   // a and b are also in order```

Popular pages Recent additions