![]() |
|
#1
|
|||
|
|||
Program Newbie: 3 numbers in ascending orderIf somebody out there could help me with this one, I'd really appreciate it...
I need a program that can list 3 user-entered integers in ascending order. If there are equal integers entered, I also need to compensate for that. My result: a complete mess. Somebody help! CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: Program Newbie: 3 numbers in ascending orderThis should help:
CPP / C++ / C Code:
Output: Quote:
|
|
#3
|
|||
|
|||
|
Much appreciated!
|
|
#4
|
||||
|
||||
Re: Program Newbie: 3 numbers in ascending orderWay too complex, and as you said, a big mess.
Since you are using only 3 values, you only need 3 if statements in the simplest solution. On paper, write down 3 'random' numbers. The figure out what you do to sort the numbers, looking at only two numbers at a time. Then convert that idea into if statements. [edit] Ahh, another don't help them, just give them the code answer. Problem is, it's also way too complex, and a mess. It can be done in 6 lines. [/edit] __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#5
|
||||
|
||||
Re: Program Newbie: 3 numbers in ascending orderWhy not use std::sort()
__________________
Gravitation is not responsible for people falling in love. -Albert Einstein |
|
#6
|
|||
|
|||
Re: Program Newbie: 3 numbers in ascending orderQuote:
Sometimes, looking at the answer is the best solution, especially in such small problems. When, people ask about such simple programs, they are just starting out with c++; and may not be able to convert an algorithm to code; doing the vivce versa is of help (my own opinion)... helps you understand the algorithm as well as teaches you to convert steps to statements. (That's the way I learnt, whatever I know about, the language.) But, as always, the initiative is on the OP, to dissect the program, and learn from it. I knowingly didn't give the better solution (using logical operators/ six statements), it is upto him/her to decide. Regards, netnut. [edit] On second thoughts, I'll refrain from posting solutions here. Maybe does more harm than good for the OP. [/edit] ...he he, borrowed your style of editing the post! |
|
#7
|
||||
|
||||
Re: Program Newbie: 3 numbers in ascending orderQuote:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#8
|
|||
|
|||
Re: Program Newbie: 3 numbers in ascending orderQuote:
nice one ! |
|
#9
|
||||
|
||||
Re: Program Newbie: 3 numbers in ascending orderQuote:
Six lines? I have a feeling that could be done, but I can't think of how. My best solution is this: Check if b > a (if so, switch values) Check if c > b (if so, switch values) Check if b > a (if so, switch values) Display a, then b, then c The last check is necessary in case c was the largest number to begin with. Walt, what's the process for the six line solution? Mine has more than six lines, because of the temporary value assignment involved in swapping numbers. Gamer_2k4 |
|
#10
|
||||
|
||||
Re: Program Newbie: 3 numbers in ascending orderQuote:
You got it. The "check" and "if so" are 2 lines. Total of 3 tests. 6 lines. I was counting only the processing lines to accomplish the task, not the output and declaration lines. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Steps to create and compile c program in SUSE | batrsau | Computer Software Forum - Linux | 7 | 03-Jun-2007 20:49 |
| Pipeline freeze simulation | darklightred | C++ Forum | 6 | 27-Jul-2006 20:37 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 09:36 |
| Amicable numbers program | noamfrie | C Programming Language | 1 | 06-Dec-2004 08:55 |
| Please Help, problems writing newbie c program | soulfly | C Programming Language | 14 | 04-Mar-2004 16:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The