•
•
•
•
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
![]() |
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Rep Power: 2
Solved Threads: 0
I need to create a decision tree that sorts any three integers in ascending order.
This is what I did:
I don't know what to do to get it to work when the numbers entered are in zyx or zxy order.
This is what I did:
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int x, y, z; cout << "Enter three integers: "; cin >> x >> y >> z; cout << "Your three integers in ascending order are: "; if(x <= y){ if(x <= z) cout << x << " "; if(y <= z) cout << y << " " << z << endl; if(z <= y) cout << z << " " << y << endl; } else if(y <= x){ if(y <= z) cout << y << " "; if(x <= z) cout << x << " " << z << endl; if(z <= x) cout << z << " " << x << endl; } else if(z <= x){ if(z <= y) cout << z << " "; if(y <= x) cout << y << " " << x << endl; if(x <= y) cout << x << " " << y << endl; } }
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...
You have to swap the values if they are not in ascending order. Something like :
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...
c Syntax (Toggle Plain Text)
if (x>y) { int tmp =x x=y y=tmp }
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...
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.
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
-- Pearl Williams
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Assigning values to Data in C/C++ (C++)
- Is This Project Possible? (Visual Basic 4 / 5 / 6)
- Still Another Boot Problem (Troubleshooting Dead Machines)
- evaluating expression using tree (C)
- Win Explorer tree flickers (Windows NT / 2000 / XP / 2003)
Other Threads in the C++ Forum
- Previous Thread: Array length
- Next Thread: linker errot



Linear Mode