4 mathematical operation
TRANSCRIPT
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 1/7
Mathematical operations with data
4 Mathematical operations on data
Learn
basic mathematical operators
precedence and associativity
operators ++ and --
type casting
4.1 Basic operators
Computers are useful tools for complex and repetitive calculations. Five basic binary
arithmetic operators are associated with mathematical operations. They are called binary
operators because two operands are reuired in the operations. The operators are tabulated
below.
!perators meanings example+ addition a+b
- subtraction a-b
" multiplication a"b
# division a#b
$ modulus a$b %gives the remainder
of the division.
4.2 Precedence and Associativity
Consider the expression
sum = 2.3 + 12*7.5/2
The expression consists of an addition operator& a multiplication operator and a division
operator. 'hich operation is performed first( The order of operations is based on some rules
of precedence and associativity.
)n C& each operator is assigned a precedence level. Multiplication and division operators have
the same precedence level which is higher than the precedence level of addition and
subtraction. *o multiplication and division are first performed.
ow between the multiplication and division operators which have the same precedence&
which is performed first(They are executed according to the order in which they occur in the expression i.e. the
associativity is from ,left to right. ence the order of operations on the expression
sum = 2.3+ 12*7.5 / 2
sum = 2.3 + 90 / 2
sum = 2.3 + 45 = 47.3
/0
12"3./ is first performed and
this gives a value of 40 whichis then divided by 2.
2.5 is then added to the
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 2/7
Mathematical operations with data
6arentheses 7 8 have the highest precedence level. Consider the expression below9
x1 = 3* (12 + 7*(4+5))
There are two parentheses % a set of nested parentheses- in this expression. The operation inthe inner most parentheses is first carried and continue outwards. ence 7:+/8 is first done&
giving the result
x1 = 3*(12+7*9)
ext the expression 12 + 3"4 within the second parentheses is evaluated. )n this expression&
the multiplication operator 7"8 has precedence over the addition operator 7+8 and hence the
resulting value is 12+;5 < 3/ . =nd finally& x1 becomes
x1 = 3*75 = 225
The table below summarises the rules precedence and associativity of some operators.
!perators =ssociativity
highest precedence 7 8 left to right
" # $ left to right
+ - left to right
lowest precedence < right to left
4.3 Writing a mathematical expression into a computer language expression.
Let>s say you wish to use the computer to evaluate the expression
a
cd b y
2
5 +=
'rite the computer expression by putting the numerator and the denominator in parentheses
and put the division operator in between as shown.
y < 7 numerator 8 # 7denominator 8
ext fill out the expressions for the numerator and denominator.
The numerator
3b + cd becomes 3*b + c*d
and the denominator
2a becomes 2*a
ow the computer expression for y is
y =(3*b + c*d) / (2*a)
/1
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 3/7
Mathematical operations with data
ere is another example. Convert the expression below to a computer expression.
5/
8127 2
−
+=
n
nT n
*tep1 9 'rite Tn < 7 8 # 7 8
*tep 29 Convert 72n+182 to 72"n + 18" 72"n +18
*tep 59 Convert /n-5 to /"n %5
*tep :9 Fill in the Tn with the numerator and the denominator expressions obtained in steps 2
and 5& i.e.Tn = ((2*n+1)*(2*n+1)) / (5*n-3)
?our turn ¬
1 'or@ out the value of the following expressions
float a<2.0& b< -5.0& c<3.0& d< -14.0A
int e < 2& f < -5& g < 3& h < - 14
Bxpression alue
a # b
c # b # a
g $ ee $ f
h # f $ e
-a " d
e # f "h
2"f + 7e + h8"2
7a-b8 - 7d+ 2"c8"7c # a 8
2 'rite an expression for total which is the sum of num_1 and num_2. )nclude the
appropriate variable declarations.
5 'rite an expression for anser which is eual to the difference of num_1 and
num_2. )nclude the appropriate variable declarations.
/2
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 4/7
Mathematical operations with data
: 'rite an expression for Ans!imes which is the result of the multiplication of num_1
and num_2. )nclude the appropriate variable declarations.
/ Convert the following mathematical expressions into C language expressions.
(a) v= u + at
(b) s= ut + ½ at 2
7c82d
abk F =
7d8a
acbb x
2
:1
2−+−
= 7Dse library function sqrt() for suare root8
← xx →
4.4 "ther operators
#ncrement operator $ %% 7double + signs8
This is a unary operatorA it reuires only one operand. )t increases the value of the variable by
1. For example& to increment the value of the variable numE1 by one we write
num_1++ ;
or
++num_1;
These two statements have the same meaning as
num_1 = num_1 +1;
&ecrement operator $ '' 7double minus signs8
The function of this operator is the opposite of operator ++. This operator decreases the value
of a variable by 1. For example& to decrease the value of the variable numE1 by 1& we write
num_1-- ;
or
/5
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 5/7
Mathematical operations with data
-- num_1
These two statements have the same meaning as
num_1 = num_1 –1;
The following program illustrates the use of these operators.
/* !"#!am 4-1 */$%nc&ud' sd%".
ma%n(),
%n abcxy;ca! c = ;
a=b=c=0; =x=y=10; a++; /*%nc!'m'n by 1 a=1 */ b++; /* b=1 */ --; /*d'c!'m'n by 1 = 9 */ x--; /*x = 9 */
c++; /* b'c"m's */
!%n (d d d d c6nabxc);
a= a+1; /* %nc!'m'n by 1 a=2 */b= b+1; /* b=2 */c = c+1; /* b'c"m's ! */
=-1; /*d'c!'m'n by 1 =8 */x=x-1; /*x=8 */!%n (ddddc6nxabc);
The screen print out will be as shown.
?our turn ¬
1 'rite a program that prompt the user for two integer numbers and then add them
together up and store the answer in another variable. 6rint out this variable. *ave
program as add.c
/:
11998822!
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 6/7
Mathematical operations with data
2 Modify the program to perform the subtraction operation. *ave program as minus.c
5 Modify the program to perform the multiplication operation. *ave program as
times.c
: Modify the program to perform the division operation. *ave program as divide.c
← xx →
4.( !ype conversion
Consider the program below
/* !"#!am 4-2 */$%nc&ud' sd%".
ma%n(), %n a=13 b=4; &"a c;
c= a/b;
!%n (d d%:%d'd by d %s 6n abc)
?our turn ¬
1 'rite out the expected screen printout in the space below.
2 Bnter the program and run it. oes the screen printout agree with the expected
screen printout of 1. )f not& list the differences.
← xx →
The screen print out will be as shown9
//
13 d%:%d'd by 4 %s 3.000000
8/14/2019 4 Mathematical Operation
http://slidepdf.com/reader/full/4-mathematical-operation 7/7
Mathematical operations with data
as the computer forgotten how to do the division( 15 divided by : should give 5.2/.
The problem is we are mixing data types in the expression
c = a/b;
The division involves two integer numbers that results in a floating point number. For integer
operation& the numbers after the decimal point are =L'=?* ignored.
The solution9 Convert the integer numbers to floating point numbers 7without changing thedeclaration statements8 Gust for this operation. This conversion of data type temporarily is
called type casting and this is how it is done.
c = (&"a)a/(&"a)b; /* c":'!s a and b " &"a "! %s "'!a%"n "n&y */
or simply
c = (&"a)a/b;
?our turn ¬
1 Ma@e the necessary modification to the program and run it again. oes it wor@ as
expected(
← xx →
/;
int intfloat