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

Thread Solved

Join Date: Nov 2009
Posts: 10
Reputation: din_hilmi is an unknown quantity at this point 
Solved Threads: 0
din_hilmi din_hilmi is offline Offline
Newbie Poster

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

 
0
  #1
Nov 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,934
Reputation: firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all 
Solved Threads: 254
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Posting Virtuoso
 
0
  #2
Nov 12th, 2009
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.
Go ahead and try to solve  this  euler problem, if u can. Check with me for hints and 
answers. Good Luck
Solved by:tux4life, zalezog, "your name here";
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 82
Reputation: samsons17 is an unknown quantity at this point 
Solved Threads: 1
samsons17 samsons17 is offline Offline
Junior Poster in Training
 
0
  #3
Nov 12th, 2009
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...

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. int a,b,c;
  7.  
  8. cout<<"Enter three integers :";
  9. cin>>a>>b>>c;
  10.  
  11.  
  12. cout<<endl;
  13. if (a>b && b>c) {
  14. cout<<c<<" "<<b<<" "<<a;
  15. }
  16.  
  17. else if (a>c && c>b)
  18. cout<<b<<" "<<c<<" "<<a;
  19.  
  20. else if (b>a && a>c)
  21. cout<<c<<" "<<a<<" "<<b;
  22.  
  23. else if (b>c && c>a)
  24. cout<<a<<" "<<c<<" "<<b;
  25.  
  26. else if (c>a && a>b)
  27. cout<<b<<" "<<a<<" "<<c;
  28.  
  29. else cout<<a<<" "<<b<<" "<<c;
  30.  
  31. cout<<endl;
  32.  
  33. if (a>b && b>c) {
  34. cout<<a<<" "<<b<<" "<<c;
  35. }
  36.  
  37. else if (a>c && c>b)
  38. cout<<a<<" "<<c<<" "<<b;
  39.  
  40. else if (b>a && a>c)
  41. cout<<b<<" "<<a<<" "<<c;
  42.  
  43. else if (b>c && c>a)
  44. cout<<b<<" "<<c<<" "<<a;
  45.  
  46. else if (c>a && a>b)
  47. cout<<c<<" "<<a<<" "<<b;
  48.  
  49. else cout<<c<<" "<<b<<" "<<a;
  50. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: din_hilmi is an unknown quantity at this point 
Solved Threads: 0
din_hilmi din_hilmi is offline Offline
Newbie Poster
 
0
  #4
Nov 16th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 82
Reputation: samsons17 is an unknown quantity at this point 
Solved Threads: 1
samsons17 samsons17 is offline Offline
Junior Poster in Training
 
0
  #5
Nov 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: din_hilmi is an unknown quantity at this point 
Solved Threads: 0
din_hilmi din_hilmi is offline Offline
Newbie Poster
 
1
  #6
Nov 16th, 2009
I still cant figure out with 5 input number..! is there anything wrong with my coding..??

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. int a, b, c, d, e;
  7. int v, w, x, y, z;
  8.  
  9. cout << "Enter Five Number: ";
  10. cin >> a >> b >> c >> d >> e;
  11.  
  12.  
  13. if ( a < b && a < c && a < d && a < e)
  14. {
  15. a = v;
  16.  
  17. }
  18. else if ( a > b && a < c && a < d && a < e )
  19. {
  20. a = w;
  21. }
  22. else if ( a > b && a > c && a < d && a < e )
  23. {
  24. a = x;
  25. }
  26. else if ( a > b && a > c && a > d && a < e )
  27. {
  28. a = y;
  29. }
  30. else if ( a > b && a > c && a > d && a > e )
  31. {
  32. a = z;
  33. }
  34. else if ( b < a && b < c && b < d && b < e )
  35. {
  36. b = v;
  37. }
  38. else if ( b > a && b < c && b < d && b < e )
  39. {
  40. b = w;
  41. }
  42. else if ( b > a && b > c && b < d && b < e )
  43. {
  44. b = x;
  45. }
  46. else if ( b > a && b > c && b > d && b < e )
  47. {
  48. b = y;
  49. }
  50. else if ( b > a && b > c && b > d && b > e )
  51. {
  52. b = z;
  53. }
  54. else if ( c < a && c < b && c < d && c < e )
  55. {
  56. c = v;
  57. }
  58. else if ( c > a && c < b && c < d && c < e )
  59. {
  60. c = w;
  61. }
  62. else if ( c > a && c > b && c < d && c < e )
  63. {
  64. c = x;
  65. }
  66. else if ( c > a && c > b && c > d && c < e )
  67. {
  68. c = y;
  69. }
  70. else if ( c > a && c > b && c > d && c > e )
  71. {
  72. c = z;
  73. }
  74. else if ( d < a && d < b && d < c && d < e )
  75. {
  76. d = v;
  77. }
  78. else if ( d > a && d < b && d < c && d < e )
  79. {
  80. d = w;
  81. }
  82. else if ( d > a && d > b && d < c && d < e )
  83. {
  84. d = x;
  85. }
  86. else if ( d > a && d > b && d > c && d < e )
  87. {
  88. d = y;
  89. }
  90. else if ( d > a && d > b && d > c && d > e )
  91. {
  92. d = z;
  93. }
  94. else if ( e < a && e < b && e < c && e < d )
  95. {
  96. e = v;
  97. }
  98. else if ( e > a && e < b && e < c && e < d )
  99. {
  100. e = w;
  101. }
  102. else if ( e > a && e > b && e < c && e < d )
  103. {
  104. e = x;
  105. }
  106. else if ( e > a && e > b && e > c && e < d )
  107. {
  108. e = y;
  109. }
  110. else if ( e > a && e > b && e > c && e > d )
  111. {
  112. e = z;
  113. }
  114.  
  115. cout << "Your Number Are: " << v << " " << w << " " << x << " " << y << " " << z;
  116.  
  117. return 0;
  118.  
  119.  
  120.  
  121.  
  122.  
  123. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,934
Reputation: firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all 
Solved Threads: 254
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Posting Virtuoso
 
0
  #7
Nov 16th, 2009
Yes, your if/else is incorrect, only 1 of them gets evaluated.
Go ahead and try to solve  this  euler problem, if u can. Check with me for hints and 
answers. Good Luck
Solved by:tux4life, zalezog, "your name here";
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 82
Reputation: samsons17 is an unknown quantity at this point 
Solved Threads: 1
samsons17 samsons17 is offline Offline
Junior Poster in Training
 
0
  #8
Nov 16th, 2009
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

  1. 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.
  1. if(a<b<c<d<e) {
  2. cout<<a<<b<<c<<d<<e;
  3.  
  4. ..........
  5. }

I hope you could understand these all...But if you still got problem,be free to ask
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: din_hilmi is an unknown quantity at this point 
Solved Threads: 0
din_hilmi din_hilmi is offline Offline
Newbie Poster
 
0
  #9
Nov 16th, 2009
so in this case just use relational operator or still got to use logical expression..??
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 82
Reputation: samsons17 is an unknown quantity at this point 
Solved Threads: 1
samsons17 samsons17 is offline Offline
Junior Poster in Training
 
0
  #10
Nov 16th, 2009
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....
Last edited by samsons17; Nov 16th, 2009 at 11:10 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2492 | Replies: 25
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC