C++ Help - School task :P

Discussion in 'Programming General' started by Dome106, Sep 28, 2010.

C++ Help - School task :P
  1. Unread #1 - Sep 28, 2010 at 7:12 AM
  2. Dome106
    Joined:
    Dec 9, 2009
    Posts:
    383
    Referrals:
    0
    Sythe Gold:
    57

    Dome106 Forum Addict
    Banned

    C++ Help - School task :P

    Well, I've got a school task and I have some problems with it.
    I need to make a program in which you put in 3 numbers and it prints out which one is the biggest. I have that done already, no problem. The problem is that when I enter 2 of the same numbers (example 5 5 1) it says that 1 is the biggest. I don't know what to do :p.

    Here's what I have:
    Code:
    int main(int argc, char *argv[])
    {
        int a, b, c;
        cin >> a >> b >> c;
        
        
        if (a > b && a > c)
        {
              cout << "A is the biggest" << endl;
        }     
        else if (b > a && b > c)
        {
             cout << "B is the biggest" << endl;
        }
        
        else
        {
            cout << "C is the biggest" << endl;
        }   
            
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    Please help me guys =)
     
  3. Unread #2 - Oct 2, 2010 at 10:43 AM
  4. xRSx Newb
    Joined:
    Sep 27, 2010
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0

    xRSx Newb Member

    C++ Help - School task :P

    Hey not sure if you still need help with this but your problem is that if two numbers are the same, your if and else if statements will both evaluate to false, meaning that the code will always execute the else statement and no matter what the inputs are, C will always be chosen as the biggest number.
     
  5. Unread #3 - Oct 2, 2010 at 11:31 AM
  6. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    C++ Help - School task :P

    If an else is not a good way to do your sorting (finding out biggest number). Look into bubble sort (inefficient but easy to understand) or if you want something efficient then look into merge sort.

    Then you can find the biggest number in an array of numbers (any size).
     
  7. Unread #4 - Oct 3, 2010 at 7:17 AM
  8. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,072
    Referrals:
    468
    Sythe Gold:
    5,287
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    C++ Help - School task :P

  9. Unread #5 - Oct 3, 2010 at 12:25 PM
  10. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    C++ Help - School task :P

  11. Unread #6 - Oct 3, 2010 at 2:39 PM
  12. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,072
    Referrals:
    468
    Sythe Gold:
    5,287
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    C++ Help - School task :P

    Virtually every language has predefined math functions.

    And it is more appropriate to use an inline for this, if you wanted to write your own. Macros can change the meaning of what you are trying to do if they're not constructed and used correctly. Inline functions give you a little more protection against this, without any function call overhead.
     
  13. Unread #7 - Oct 3, 2010 at 3:58 PM
  14. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    C++ Help - School task :P

    Typically macro problems can be addressed by preprocessing to a file and taking a direct look at what is actually being done. You're right, though, now that I think about it, inline functions would be better.
     
  15. Unread #8 - Oct 4, 2010 at 6:11 AM
  16. Dome106
    Joined:
    Dec 9, 2009
    Posts:
    383
    Referrals:
    0
    Sythe Gold:
    57

    Dome106 Forum Addict
    Banned

    C++ Help - School task :P

    Thanks Sythe, helped me a lot =).
     
  17. Unread #9 - Oct 20, 2010 at 1:51 PM
  18. Ashken
    Joined:
    Dec 18, 2009
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    Ashken Newcomer

    C++ Help - School task :P

    Code:
    
    int maximum( int,int int);
    
    int main()
    {
       int a,b,c;
       cin>> a>>b>>c;
       cout<<"maximum value is "<<maximum(a,b,c);
    return o;
    }
    int maximum(int x,int y,int z)
    {
       int maxVal = x;
    
       if (y > maxVal)
         maxVal = y;
    
      if (z > maxVal)
        maxVal = z;
    
     return maxVal;
    }
    
    
     
< [source]Runescape Browser[source] | A Simple Hello world Tutorial for Java! Part 1 of many. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site