# Sorting numbers

Page 1 of 1

## 1 Replies - 1420 Views - Last Post: 21 November 2009 - 10:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=140736&amp;s=246a1f1cc00d5be6add15079e1e3bb23&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 light09

Reputation: 0
• Posts: 141
• Joined: 21-November 09

# Sorting numbers

Posted 21 November 2009 - 10:26 PM

#include <iostream>
using namespace std;
int main()
{

int num1, num2, num3, num4, num5, r, i, integers, median;

cout << " Enter 5 integers :";
cin >> num1 >> num2 >> num3 >> num4 >> num5;

if ( a < b )
{
cin >> a;
else
cin >> b;
}
if ( a < c )|| ( b < c))
{
cin >> a >> b >> c;
else
cin >> b >> a >> c;
else
cin >> a >> c >> b;
else
cin >> c >> a >> b;

}
if ( c < d ) || ( b < d )
{
cin >> a >> b >> c >> d;
else
cin >> b >> a >> c >> d;
else
cin >> a >> c >> b >> d;

i find sorting numbers in this manner inefficient as you need to compare for five numbers. i am wondering if there is a possibility of simplifying this code by converting it into a loop. Is it possible?

Admin Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,
PsychoCoder

Is This A Good Question/Topic? 0

## Replies To: Sorting numbers

### #2 Martyr2

• Programming Theoretician

Reputation: 4867
• Posts: 13,189
• Joined: 18-April 07

## Re: Sorting numbers

Posted 21 November 2009 - 10:50 PM

Well it is inefficient because you have forbidden anything that would simplify it. Arrays, functions, pointers STL etc. Without any of that you are pretty much left with comparing values against one another.... a > b? a > c? a > d etc.

You could do this using a series of if statements or to compact it down, you might want to try a series of ternary operators. I don't know why you can't use at least functions you write.