Thread: Newbie : Getting the average of 3 numbers

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Newbie : Getting the average of 3 numbers

    New here, 'Hi' to all.

    I'm, starting a Software Engineering degree at Uni this October and so am trying to teach myself C++ in preparation.

    Anyway.

    I'm writing this programme to find the average of 3 numbers.

    PHP Code:
    #include <iostream>

    using namespace std;

    int main()
    {
        
    int numberone,numbertwo,numberthree,numberaverage

        
        
    cout<<"Please enter the first number: ";
        
    cin>> numberone
        
    cin.ignore(); 
        
    cout<<"Please enter the second number: "
        
    cin>> numbertwo
        
    cin.ignore(); 
        
    cout<<"Please enter the third number: ";
        
    cin>> numberthree;
        
    cin.ignore();
        
        
    numberaverage numberone numbertwo numberthree 3
        
    cout <<"The average is " <<numberaverage
        
    cin.get(); 
    Everything is working fine apart from this line:-
    PHP Code:
    numberaverage numberone numbertwo numberthree 3
    It is supposed to divide the sum total of the 3 integers/variables by 3 to give the average, but it is not.

    For example 10 + 20 + 30 is giving 40 as the answer, not 20.

    What should I be doing?

    Thanks very much!

    Swerve

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Think about operator precedence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks for giving me a clue Salem .

    Looking at a Google of Operator Precedence it seems that the order of the line is incorrect...?

    I'm thinking that / has a higher precedence than +

    Perhaps the line should be something like :-

    Code:
    numberaverage = 3 / {numberone + numbertwo + numberthree};


    Thank you.

    EDIT - This new line does not work, nor does:-

    PHP Code:
    numberaverage = {numberone numbertwo numberthree} / 3
    but am I on the 'right road'?


    Thanks.
    Last edited by Swerve; 08-27-2007 at 03:26 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, try ( )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    OK, i give up, I've tried placing the () and ( code here ) everywhere I can think of.

    Could someone tell me what the line should be please.

    Please don't be totally elitist straight away.

    Thanks.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Swerve View Post
    OK, i give up, I've tried placing the () and ( code here ) everywhere I can think of.

    Could someone tell me what the line should be please.

    Please don't be totally elitist straight away.

    Thanks.
    What does your current "average = " line look like, and what is the result you are getting, and how is that different from what you expect?

    Have you tried splitting it into two separate lines (statements) of calculation before you try adding parenthesis?

    --
    Mats

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > For example 10 + 20 + 30 is giving 40 as the answer, not 20.
    So you would agree that 1, 2, and 3 average out to 2.
    Code:
    #include <iostream>
    
    int main ( void ) 
    {
      std::cout << ( 1 + 2 + 3 ) / 3 << '\n';
      return 0;
    }
    C:\Documents and Settings\Owner\cbproject\bar\windows\Debug_Build\b ar.exe
    2

    I think you had it right when you enclosed the divedend in parentheses.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Quote Originally Posted by matsp View Post
    What does your current "average = " line look like, and what is the result you are getting, and how is that different from what you expect?

    Have you tried splitting it into two separate lines (statements) of calculation before you try adding parenthesis?

    --
    Mats
    Thanks matsp ! Splitting it up worked.

    The working version is done like this :-

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int numberone,numbertwo,numberthree,numbertotal,numberaverage; 
    
        
        cout<<"Please enter the first number: ";
        cin>> numberone; 
        cin.ignore(); 
        cout<<"Please enter the second number: "; 
        cin>> numbertwo; 
        cin.ignore(); 
        cout<<"Please enter the third number: ";
        cin>> numberthree;
        cin.ignore();
        numbertotal = numberone + numbertwo + numberthree;
        cout<<"The total is " <<numbertotal; 
        numberaverage = numbertotal / 3;
        cout<<"\nThe average number is " <<numberaverage;
        
        cin.get(); 
    }
    Is it possible to add the sum of 1,2 and 3 and divide them by 3 with just one line though, or is it best practice to divide up the script into adding for the sum, and Then dividing ?

    Thanks.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Is it possible to add the sum of 1,2 and 3 and divide them by 3 with just one line though
    Yes, you were on the right track earlier but you apparently didn't understand Salem's hint and gave up.
    Code:
    numberaverage = {numberone + numbertwo + numberthree} / 3;
    That is close, except you shouldn't be using curly braces, you should be using parentheses.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In general, splitting up is often preferable simply because it's more readable. It's like a series of simple sentences is easier to understand than one complex sentence, with insertions, descriptive side sentences and all that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    In general, splitting up is often preferable simply because it's more readable. It's like a series of simple sentences is easier to understand than one complex sentence, with insertions, descriptive side sentences and all that.
    Agreed for complex statements (and it probably makes NO difference at all to the compiler if you write an expression as 6 different sub-calculations or one single statement with 6 sub-parts in it). It obviously does make a difference if a subsequenet read of the code can be done without being completely confusing or not.

    The other side is of course that if you have some really simple calculation (e.g. an "average of two"), it makes sense to do that on a single line:
    Code:
    c = (a + b) / 2;
    Splitting that would just make it LESS readable.

    Also, adding extra parenthesis to clarify which order you want things done never hurts - obviously within reason.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in finding average of a class of numbers
    By hastefan2001 in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2009, 01:11 PM
  2. What's wrong with my Average Array program?
    By special1zed in forum C Programming
    Replies: 12
    Last Post: 03-15-2009, 11:01 PM
  3. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 03:24 PM
  4. how unlimited the amount of numbers to average
    By Machewy in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2003, 08:44 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM