hello, i'm newbie here and also in programming world. so I would to request help from pros here. my question is, how to print out number entered in random in ascending order WITHOUT USING ARRAY AND FUNCTION? let say the user key in 4, 56, 31, 90, 11 and the output would be 4, 11, 31, 56, 90. hope anyone would help me.
thank you.

1) Why ?

2) need compile time variable meaning there will be a fixed amount
of inputs to get

3) Need to implement your own sorting method and use it inside main,
so technically it won't be a function. If not then you will need a lot
of if/else or you can use trees.

4) Bad Idea.

din....

more or less you have to use lots of if else...
but this is suitable only if you get fixed number of input..
let say that you ask the user to enter only 3 numbers only..
by manipulating the input using if else statement,you could sorting them
in the way you like...
consider this example of program that sorting the numbers user entered in ascending or decending order...

#include <iostream>
using namespace std;

int main ()
{
    int a,b,c;

    cout<<"Enter three integers :";
    cin>>a>>b>>c;


cout<<endl;
    if (a>b && b>c) {
    cout<<c<<" "<<b<<" "<<a;
    }

    else if (a>c && c>b)
    cout<<b<<" "<<c<<" "<<a;

    else if (b>a && a>c)
    cout<<c<<" "<<a<<" "<<b;

    else if (b>c && c>a)
    cout<<a<<" "<<c<<" "<<b;

    else if (c>a && a>b)
    cout<<b<<" "<<a<<" "<<c;

    else cout<<a<<" "<<b<<" "<<c;

cout<<endl;

    if (a>b && b>c) {
    cout<<a<<" "<<b<<" "<<c;
    }

    else if (a>c && c>b)
    cout<<a<<" "<<c<<" "<<b;

    else if (b>a && a>c)
    cout<<b<<" "<<a<<" "<<c;

    else if (b>c && c>a)
    cout<<b<<" "<<c<<" "<<a;

    else if (c>a && a>b)
    cout<<c<<" "<<a<<" "<<b;

    else cout<<c<<" "<<b<<" "<<a;
}

huhuhu.. my head already mess with <i>if else</i>.. thanx samsons 17.. its just 3 number while my assignment need 5 number but really help me to discover it.. thanx a lot dude..

happy to hear that u have discover it :)
actually the if/else is just basic which u have to practice to master it...
once u have fully understanding about it,then u should have no more problems...
Happy Coding :)

I still cant figure out with 5 input number..! is there anything wrong with my coding..??

#include <iostream>
using namespace std;

int main ()
{
	int a, b, c, d, e;
	int v, w, x, y, z;
	
	cout << "Enter Five Number: ";
	cin >> a >> b >> c >> d >> e;
	
	
	if ( a < b && a < c && a < d && a < e)
	{
		a = v;
		
	}
	else if ( a > b && a < c && a < d && a < e )
	{
		a = w;
	}
	else if ( a > b && a > c && a < d && a < e )
	{
		a = x;
	}
	else if ( a > b && a > c && a > d && a < e )
	{
		a = y;
	}
	else if ( a > b && a > c && a > d && a > e )
	{
		a = z;
	}
	else if ( b < a && b < c && b < d && b < e )
	{
		b = v;
	}
	else if ( b > a && b < c && b < d && b < e )
	{
		b = w;
	}
	else if ( b > a && b > c && b < d && b < e )
	{
		b = x;
	}
	else if ( b > a && b > c && b > d && b < e )
	{
		b = y;
	}
	else if ( b > a && b > c && b > d && b > e )
	{
		b = z;
	}
	else if ( c < a && c < b && c < d && c < e )
	{
		c = v;
	}
	else if ( c > a && c < b && c < d && c < e )
	{
		c = w;
	}
	else if ( c > a && c > b && c < d && c < e )
	{
		c = x;
	}
	else if ( c > a && c > b && c > d && c < e )
	{
		c = y;
	}
	else if ( c > a && c > b && c > d && c > e )
	{
		c = z;
	}
	else if ( d < a && d < b && d < c && d < e )
	{
		d = v;
	}
	else if ( d > a && d < b && d < c && d < e )
	{
		d = w;
	}
	else if ( d > a && d > b && d < c && d < e )
	{
		d = x;
	}
	else if ( d > a && d > b && d > c && d < e )
	{
		d = y;
	}
	else if ( d > a && d > b && d > c && d > e )
	{
		d = z;
	}
	else if ( e < a && e < b && e < c && e < d )
	{
		e = v;
	}
	else if ( e > a && e < b && e < c && e < d )
	{
		e = w;
	}
	else if ( e > a && e > b && e < c && e < d )
	{
		e = x;
	}
	else if ( e > a && e > b && e > c && e < d )
	{
		e = y;
	}
	else if ( e > a && e > b && e > c && e > d )
	{
		e = z;
	}
	
	cout << "Your Number Are: " << v << " " << w << " " << x << " " << y << " " << z;
		
	return 0;
	 
	
	


}

okeyh...
Its look like you still not getting the idea...
Sorry for saying this,but your code its completely wrong if its intended to do like what you told in this post..

In your code that you have done,you only evaluate the variable "a" with the other variables that you have declared,which is of course it is wrong..and then in the end of your code,you wrote

cout << "Your Number Are: " << v << " " << w << " " << x << " " << y << " " << z;

which is simply print all those variables,thus your if/else statement before were all useless...

What you should actually do is cin>> all the five numbers entered by user into 5 variables like a,b,c,d,e...
Then you may use the operator to compare those 5 variables...
The first step you should do is by sorting them manually in a piece of paper before you do your program...The sorting will be like this :

a b c d e
a b c e d
a c b d e
a c b e d
a d b c e
a d b e c
a e b c d
a e b d c
b a c d e
b a c e d
b c a d e
b c a e d
b d a c e
b d a e c
b e a c d
b e a d c
c a b d e
c a b e d
c b a d e
c b a e d
c d a b e
c d a e b
c e a b d
c e a d b
d a b c e
d a b e c
d b a c e
d b a e c
d c a b e
d c a e b
d e a b c
d e a c b
e a b c d
e a b d c
e b a b c
e b a c b
e c a b d
e c a d b
e d a b c
e d a c b

After sorting them like this..I advice that you write the code for cout<<
first,an then only you compare them using the operator...

The coding is just the same like what i've shown you in the previous reply..
eg.

if(a<b<c<d<e) {
cout<<a<<b<<c<<d<<e;

..........
}

I hope you could understand these all...But if you still got problem,be free to ask :)

so in this case just use relational operator or still got to use logical expression..??

i'm sorry for my mistake....

you have to use logical expression and thus cannot simply write
if(a<b<c<d<e) like that....

Edited 7 Years Ago by samsons17: n/a

is it something like this exist..??

if ( a < b && a < c && a < d && a < e)

is it possible to use 'switch' or 'for' statement in this problem..??

It should be more or less like this :

if (a<b && b<c && c<d && d<e)
cout<<a<<b<<c<<d<<e;

Nope...u cannot compare or inserting the range when using the switch..

Five numbers can be ordered 5! or 120 ways. The no-brainer way of writing this code would thus be writing an if statement with 119 "else if" statements, one for each possible ordering. You should be using <= instead of < (what if two elements are the same?). There may be some clever nested-if ways of doing this that don't require 120 if statements of some sort. I suppose that might be interesting to have a contest where you had to sort 5 numbers without using arrays or functions and people might come up with creative answers.

But the larger question is this: why are you adding these stipulations? There's a reason why all the sorting algorithms out there use arrays and/or helper functions/recursion. Why would anyone want to write 120 if-statements?

quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<

well since since there are 5! possible combination, the minimum
number of if stated you will use is 2^5 = 32.

That will suck.

Just use insertion sort, the idea behind it is to mid a max* element
and swap it with the last element. Then repeat it with the last element
decreasing by 1 each time.

I didnt get it 1stperson.. mind to show me..??

In psuedocode of course :

void insertionSort(int * Array, int size)
{
	
	//declare minElem variable to 0

	//for i = 0 to i < size, increment i by 1
	{
		//set minElem to i 
              //for j = i +1 to j < size; increment j by 1
                {
                         //if array[j] is less than array[minElem]
                              //then set minElem to j
                  }
             //now swap array at i with array at minElem
	}
}

quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<

"Simple", "efficient", "correct", and "not tedious" are different concepts. Your "easiest", most brain-dead method is to make 120 "if" statements, not taking advantage of the results of previous tests. The CORRECT way is to use an array, function, or whatever is needed. Back to my original question. WHY are you limited with this restriction? What's your experience level? One can always avoid arrays by using pointer arithmetic in lieu of the [] operator, which is basically the same thing, so what's the point? Sounds like one of those classes where they require you to do it the WRONG way so you'll appreciate the RIGHT way when they get to teaching it. If that's the case, just write your 120 if-statements and get it over with, I guess.

By the way, have you written it for sorting two, three, and four numbers? That comes before five. Get the pattern down.

Comments
Excellent advice.

quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<

I didnt get it 1stperson.. mind to show me..??

I'm looking at the post times. This one comes two posts after firstPerson's post. Quite fast. I wouldn't "get it" either if I had to learn the Insertion Sort in two minutes. Since it uses an array, you can't use it anyway. No one's going to give you the code without some effort on your part.

I wrote something for this and if you want to put in more numbers its really easy (not as easy if you would just use arrays) if you read the code and see what I'm doing.

#include <iostream>

using namespace std;

int main()
{
	int n1, n2, n3, n4, n5, a, b;
	
	cout << "input n1: ";
	cin >> n1;
	cout << "input n2: ";
	cin >> n2;
	cout << "input n3: ";
	cin >> n3;
	cout << "input n4: ";
	cin >> n4;
	cout << "input n5: ";
	cin >> n5;
	
	for( int i = 1; i <= 5; i++ )
	{
		switch(i)
		{
			case 1:
				a = n1;
				break;
			case 2:
				a = n2;
				break;
			case 3:
				a = n3;
				break;
			case 4:
				a = n4;
				break;
			case 5:
				a = n5;
				break;
		}
		for( int c = 1; c <= 5; c++ )
		{
			switch(c)
			{
				case 1:
					b = n1;
					break;
				case 2:
					b = n2;
					break;
				case 3:
					b = n3;
					break;
				case 4:
					b = n4;
					break;
				case 5:
					b = n5;
					break;
			}
			if( b > a )
			{
				int temp = a;
				a = b;
				b = temp;
			}
			
			switch(i)
			{
				case 1:
					n1 = a;
					break;
				case 2:
					n2 = a;
					break;
				case 3:
					n3 = a;
					break;
				case 4:
					n4 = a;
					break;
				case 5:
					n5 = a;
					break;
			}
			
			switch(c)
			{
				case 1:
					n1 = b;
					break;
				case 2:
					n2 = b;
					break;
				case 3:
					n3 = b;
					break;
				case 4:
					n4 = b;
					break;
				case 5:
					n5 = b;
					break;
			}

		}
		
	}
	
	cout << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << endl;
	
	system("PAUSE");
	return 0;
}

sfuo, thanks very much! I really bad in making switch statement and perhaps you may put comments in this code to make me easier to read and alter it to use in my programming.. you really helpful.. thanx again..

What have you really learned in the class so far, what concepts?

You haven't learned about functions or you don't want to use functions?

Can you use the standard functions?

Think of all possible ways you can do this before using the minimum of
32 if statements.

sfuo, thanks very much! I really bad in making switch statement and perhaps you may put comments in this code to make me easier to read and alter it to use in my programming.. you really helpful.. thanx again..

He just gave you the entire program and now you want comments (very nice program by the way, sfuo. It works well). Why don't you go through the code, figure out how it works, and after you do, comment it yourself?

i already can figure out by myself btw.. thanx 4 criticize me vernon.. I know I would never become a good programmer if continue being feed up like this but somehow, I cant really think properly under stress condition and somewhat, I would try to improve it inches by inches.. I take this as a great lesson to me to think outside the box in every problem solution in future..

sfuo, nice...
I always use array for this kind of problem. So I also wondering on how to do that without array.

din_hlmi
your due date is this friday right?... hahaha I know it...

This question has already been answered. Start a new discussion instead.