Sorting numbers

Sorting numbers without arrays, pointers, STL, and function

Page 1 of 1

1 Replies - 1293 Views - Last Post: 21 November 2009 - 10:50 PM Rate Topic: -----

#1 light09  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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 => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting numbers

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4358
  • View blog
  • Posts: 12,176
  • 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. :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1