GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Aug-2006, 23:50
You Look Bored You Look Bored is offline
New Member
 
Join Date: Jul 2006
Location: WV
Posts: 10
You Look Bored is on a distinguished road

Program Newbie: 3 numbers in ascending order


If 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:
//Daniel D. 08 02 06
//This program takes 3 user-entered integers, then lists them in ascending order.

#include <iostream>

using namespace std;

int main ()
{
    int x;
    int y;
    int z;
    
    cout << "This program places 3 integers in ascending order." << endl;
    cout << "Type 3 different integer values, pressing Enter after each one." << endl;
    cin >> x;
    cin >> y;
    cin >> z;
    
    if (x <= y) {                  //level 1
 /*level 2 */if (x < y) {
 /*level 3*/  if ( y < z) {
                     cout << x << "," << y << "," << z << endl;}}
                else if (y = z) {
                     cout << x << "," << y << "," << z << endl;}
                     else if (x < z) {
                          cout << x << "," << z << "," << y << endl;}
                          else if (x = z) {
                               cout << x << "," << z << "," << y << endl;}
                               else {cout << z << "," << x << "," << y << endl;}
                          if (x > z) {
                                cout << z << "," << x << "," << y << endl;}
                                else {cout << z << "," << x << "," << y << endl;}
/*level 3*/  if (x = y) {
                if (y < z) {
                      cout << x << "," << y << "," << z << endl;}
                      else if (y = z) {
                           cout << x << "," << y << "," << z << endl;}
                           else {cout << x << "," << z "," << y << endl;}
                      else if (y = z) {
                           cout << x << "," << y << "," << z << endl;}
                           else {cout x << "," << y << "," << z << endl;}
                      else if (y > z) {
                           cout << z << "," << y << "," << x << endl;}
                           else if (y = z) {
                                cout x << "," << y << "," << z << endl;}
                                else {cout << x << "," << y "," << z << endl;}
                           else if (y < z) {
                                cout x << "," << y << "," << z << endl;}
                                else {cout x << "," << y << "," << z << endl;}}
                      else if (y = z){
                           cout x << "," << y << "," << z << endl;}
                           else if (y < z){
                                cout << x << "," << y << "," << z << endl;}
                                else if (y < z) {
                                     cout x << "," << y << "," << z << endl;}}
/*level 2*/if (y <= z){
/*level 3*/  if (y < z) {
                cout << x << "," << y "," << z << endl;}
             if (y = z) {
                   cout << y "," << z << "," << x << endl;}}
             else {cout << z << "," y << "," << x << endl;}}
             
             return 0;
             }
                   
  #2  
Old 02-Aug-2006, 00:20
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Program Newbie: 3 numbers in ascending order


This should help:

CPP / C++ / C Code:
#include<iostream>
using namespace std;
int main(){
   int a, b, c;
   cout<<"Enter 3 numbers: ";
   cin>>a>>b>>c;
   cout<<"Numbers in ascending order: ";
   if(a<b){
        /*a is less than b*/
        if(a<c){
            /* a is less than b AND c => a is smallest - print it*/
            cout<<a<<", ";
            
            /* comparing c and b now, a has already been dealt with*/
            if(c<b)
            cout<<c<<", "<<b<<endl;
            else cout<<b<<", "<<c<<endl;
        }
        else{
            /* a is less than b, but c is still lesser => c is least - print it*/
            cout<<c<<", ";
            
            /*now deal with reamining a and b*/
            if(a<b) cout<<a<<", "<<b<<endl;
            else cout<<b<<", "<<a<<endl;
        }
    }
    else{
        /*b is less than a*/
        if(b<c){
            /*b is less than a AND c => b is least, print it*/
            cout<<b<<", ";
            
            /*deal with other two i.e. a and c*/
            if(a<c) cout<<a<<", "<<c<<endl;
            else cout<<c<<", "<<a<<endl;
        }
        else{
            /*b is less than a, but c is still lesser => c is least, print it*/
            cout<<c<<", ";
            
            /*deal with remaining a & b*/
            if(a<b) cout<<a<<", "<<c<<endl;
            else cout<<b<<", "<<a<<endl;
        }
    }
    return 0;
}

Output:
Quote:
Enter 3 numbers: 2 1 3
Numbers in ascending order: 1, 2, 3

Enter 3 numbers: 5 6 4
Numbers in ascending order: 4, 5, 6

Enter 3 numbers: 9 7 8
Numbers in ascending order: 7, 8, 9
  #3  
Old 02-Aug-2006, 00:33
You Look Bored You Look Bored is offline
New Member
 
Join Date: Jul 2006
Location: WV
Posts: 10
You Look Bored is on a distinguished road
Much appreciated!
  #4  
Old 02-Aug-2006, 01:19
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Program Newbie: 3 numbers in ascending order


Way 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  
Old 02-Aug-2006, 01:22
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: Program Newbie: 3 numbers in ascending order


Why not use std::sort()
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
  #6  
Old 02-Aug-2006, 01:33
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Program Newbie: 3 numbers in ascending order


Quote:
Originally Posted by WaltP
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.


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  
Old 02-Aug-2006, 02:29
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Program Newbie: 3 numbers in ascending order


Quote:
Originally Posted by 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!
I stole -- er, borrowed it from someone else....
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 03-Aug-2006, 02:49
globe globe is offline
New Member
 
Join Date: Aug 2006
Posts: 21
globe has a little shameless behaviour in the past

Re: Program Newbie: 3 numbers in ascending order


Quote:
Originally Posted by netnut
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!


nice one !
  #9  
Old 03-Aug-2006, 11:29
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: Program Newbie: 3 numbers in ascending order


Quote:
Originally Posted by WaltP
Way too complex, and as you said, a big mess.

SNIP

Problem is, it's also way too complex, and a mess. It can be done in 6 lines.

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  
Old 03-Aug-2006, 13:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Program Newbie: 3 numbers in ascending order


Quote:
Originally Posted by Gamer_2k4
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

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 GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 06:58.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.