# Newbie : Getting the average of 3 numbers

This is a discussion on Newbie : Getting the average of 3 numbers within the C++ Programming forums, part of the General Programming Boards category; New here, 'Hi' to all. I'm, starting a Software Engineering degree at Uni this October and so am trying to ...

1. ## 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

3. 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.

4. Yes, try ( )

5. 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. Originally Posted by Swerve
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. > 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. Originally Posted by matsp
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. >> 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. 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.

11. Originally Posted by CornedBee
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