Avarage calculator

Aug 25, 2010 at 1:39pm
Hey!
I was doing some stuff and i made this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
int main() {
	int a,b,c;

		cout<<"Write two numbers that you want to get avarage"<<endl;
	cin>>a;
	cin>>b;
	

	c=a + b/2;

		cout<<"Your number is: "<<c<<endl;

	char z;
	cin>> z;

	return 0;
}



I get random number... i think c=a + b/2; doesn't work good.


Ty for help!
Aug 25, 2010 at 1:40pm
search about operator precedence and integer division
Aug 25, 2010 at 1:48pm
what what what? i am new to whole c++ thing ^^
Aug 25, 2010 at 2:04pm
Those are about mathematics
Aug 25, 2010 at 3:33pm
Or just add brackets around a+b... Mathematics rules state that you divide before adding.. Brackets however are to be solved before going on with the rest..

In other words, by adding brackets around the a+b, it'll first add those, and then divide it by 2 :)
Aug 25, 2010 at 3:38pm
kaduuk right u need to add an Brackets at a+b. because in the MDAS rule. . .
Aug 25, 2010 at 4:02pm
ty guys
Topic archived. No new replies allowed.