954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION

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.

din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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;
}
samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

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

din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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 :)

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

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;
	 
	
	


}
din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Yes, your if/else is incorrect, only 1 of them gets evaluated.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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 :)

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

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

din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

i'm sorry for my mistake....

you have to use logical expression and thus cannot simply write
if(a

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

is it something like this exist..??

if ( a < b && a < c && a < d && a < e)
din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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

din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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..

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

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?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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

din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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

din_hilmi
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
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
	}
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You