Sie sind auf Seite 1von 7

Mathematical operations with data

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 re uired in the operations. The operators are tabulated below. !perators + " # $ meanings addition subtraction multiplication division modulus example a+b a-b a"b a#b 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
12"3./ is first performed and this gives a value of 40 which is then divided by 2. 2.5 is then added to the result .

sum = 2.3

90 / 2

sum = 2.3 + 45 = 47.3

/0

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 in the 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. highest precedence lowest precedence !perators 78 " # $ + < =ssociativity left to right left to right left to right 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
y= 5b + cd 2a

'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

Mathematical operations with data

.ere is another example. Convert the expression below to a computer expression.


Tn = 7 2n + 18 2 /n 5

*tep1 9 'rite *tep 29 Convert *tep 59 Convert

Tn < 7 72n+182 /n-5

8 #7 to 72"n + 18" 72"n +18 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 a#b c#b#a g$e e$f h#f$e -a " d e # f "h 2"f + 7e + h8"2 7a-b8 - 7d+ 2"c8"7c # a 8 2 Calue

'rite an expression for total which is the sum of num_1 and num_2. )nclude the appropriate variable declarations.

'rite an expression for ans er which is e ual to the difference of num_1 and num_2. )nclude the appropriate variable declarations.

/2

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 + at2
ab d2

7c8 F = k

7d8 x1 =

b + b 2 :ac 2a

7Dse library function sqrt() for s uare root8

xx 4.4 "ther operators #ncrement operator $ %% 7double + signs8 This is a unary operatorA it re uires 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

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' (s)d%".*+ ma%n() , %n) a-b-c-.-x-y; c*a! c* = /01; 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*++; /* 0 b'c"m's 2 */ 0!%n)3 (45d- 5d- 5d- 5d- 5c6n7-a-b-.-x-c*); a= a+1; /* %nc!'m'n) by 1- a=2 */ b= b+1; /* b=2 */ c* = c*+1; /* 2 b'c"m's ! */ .=.-1; /*d'c!'m'n) by 1- .=8 */ x=x-1; /*x=8 */ 0!%n)3 (45d-5d-5d-5d-5c6n7-.-x-a-b-c*); 9

The screen print out will be as shown.


1-1-9-9-2 8-8-2-2-!

?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

/:

Mathematical operations with data

Modify the program to perform the subtraction operation. *ave program as minus.c 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' (s)d%".*+ ma%n() , %n) a=13- b=4; 3&"a) c; c= a/b; 9 0!%n)3 (45d d%:%d'd by 5d %s 536n7- a-b-c)

?our turn 1 'rite out the expected screen printout in the space below.

Bnter the program and run it. Foes 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

//

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;
float int int

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 the declaration statements8 Gust for this operation. This conversion of data type temporarily is called type casting and this is how it is done.
c = (3&"a))a/(3&"a))b; /* c":'!)s a and b )" 3&"a) 3"! )*%s "0'!a)%"n "n&y */

or simply c = (3&"a))a/b; ?our turn 1

Ma@e the necessary modification to the program and run it again. Foes it wor@ as
expected(

xx

/;

Das könnte Ihnen auch gefallen