User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,423 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,615 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1622 | Replies: 3
Reply
Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

decision tree

  #1  
Apr 5th, 2007
I need to create a decision tree that sorts any three integers in ascending order.
This is what I did:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int x, y, z;
  7. cout << "Enter three integers: ";
  8. cin >> x >> y >> z;
  9. cout << "Your three integers in ascending order are: ";
  10.  
  11. if(x <= y){
  12. if(x <= z) cout << x << " ";
  13. if(y <= z) cout << y << " " << z << endl;
  14. if(z <= y) cout << z << " " << y << endl;
  15. }
  16. else
  17. if(y <= x){
  18. if(y <= z) cout << y << " ";
  19. if(x <= z) cout << x << " " << z << endl;
  20. if(z <= x) cout << z << " " << x << endl;
  21. }
  22. else
  23. if(z <= x){
  24. if(z <= y) cout << z << " ";
  25. if(y <= x) cout << y << " " << x << endl;
  26. if(x <= y) cout << x << " " << y << endl;
  27. }
  28.  
  29. }
I don't know what to do to get it to work when the numbers entered are in zyx or zxy order.
Last edited by WaltP : Apr 6th, 2007 at 2:20 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 4
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: decision tree

  #2  
Apr 5th, 2007
You have to swap the values if they are not in ascending order. Something like :
  1. if (x>y)
  2. { int tmp =x
  3. x=y
  4. y=tmp
  5. }
etc...
It wasn't clear whether the you just wanted to output the numbers in ascending order, or you had to maintain the variable name as well. Seems that don't
so a simple variable swap should be OK
You get the idea...
Last edited by WaltP : Apr 6th, 2007 at 2:20 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Reply With Quote  
Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

Re: decision tree

  #3  
Apr 5th, 2007
i don't understand what you're saying to do. can you clarify? the variable names do not need to stay the same.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: decision tree

  #4  
Apr 6th, 2007
All you're trying to do is output the values in order. That's not a sort.

Check if x > y. If so, move x into y and y into x (switch the values).

Then do it again for y and z.

You'll have to go back and retest x and y again.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC