View
3
Download
0
Category
Preview:
Citation preview
3/5/2019
1
CHAPTER 5:
ARITHMETIC EXPRESSIONS
1
2
In this chapter, you will learn:
• The built-in operator for specific mathematical
or logical manipulations.
• The evaluation of operators in arithmetic
expressions in a precise sequence determined
by the rules of operator precedence
• The expressions using the mathematical
operators.
• Some C mathematical operations functions in
the standard library.
Chapter 5 Objectives
3
5.1 Introduction
5.2 Operators in C
5.3 Mathematical Expressions
5.4 Data Type Conversion
5.5 Mathematical Library Function
5.6 Summary
Chapter 5 Contents
4
Chapter 5 5.1 Introduction
• In mathematics, an expression (or mathematical expression)
is a finite combination of symbols that is well-formed
according to rules that depend on the context.
• Mathematical symbols can assign numbers (constants),
variables, operations, functions, punctuation, grouping, and
other aspects of logical syntax.
• Mathematical expressions include arithmetic expressions
that express that results in a numeric values: integer and
floating point numbers.
• The simplest arithmetic expressions are literals number and
variables.
3/5/2019
2
5
Chapter 5 5.1 Introduction
• Most C programs perform calculations using the C arithmetic
operators
• Note: the use of various special symbols not used in algebra.
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
6
5.1 Introduction
5.2 Operators in C
• Operators precedence
• Sample algebraic and C expression
5.3 Mathematical Expressions
5.4 Data Type Conversion
5.5 Mathematical Library Function
5.6 Summary
Chapter 5 Contents
7
Chapter 5 5.2 Operators in C
• An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations.
• Used to perform operations on data.
• C language is rich in built-in operators and provides the
following types of operators:
• Arithmetic operators
• Assignment operators
• Relational operators
• Equality operators
• Logical operators
• Increment / Decrement operators
• Bitwise operators
8
• Few symbols in assignment statements which have been
used in programs.
• Assume, int a = 4, b = 5, d;
C Operation Arithmetic
Operator C Expression
Value of d after
assignment
Addition
+
d = a + b
9
Substraction
-
d = b - 2
3
Multiplication
*
d = a * b
20
Division
/
d = a/2
2
Modulus
%
d = b%3
2
Chapter 5 5.2 Operators in C
Arithmetic operators
3/5/2019
3
9
Chapter 5 5.2 Operators in C
• Assume, int x = 4, y = 5, z = 8;
Assignment
Operator
Sample
Expression
Similar
Expression
Value of variable
after assignment
+=
x += 5
x = x + 5
x = 9
-=
y -= x
y = y - x
y = 1
*=
x *= z
x = x * z
x = 32
/=
z /=2
z = z / 2
z = 4
%=
y %=x
y = y % x
y = 1
Assignment operators
10
• Assume, int y = 6, x = 5;
Relational Operators
Sample Expression
Value
>
y > x
T
<
y < 2
F
>=
x >= 3
T
<=
y <= x
F
Equality Operators
Sample Expression
Value
==
x == 5
T
! =
y ! = 6
F
Chapter 5 5.2 Operators in C
Relational and Equality operators
11
Assume,
int x = 50;
Logical Operators Called Sample Operation
&&
AND
expression1 && expression 2
||
OR
expression1 || expression2
!
NOT
! expression
Expression !Expression Sample Expression
F
T
!(x == 60)
T
F
!(x != 60)
Chapter 5 5.2 Operators in C
Logical operators
12
Chapter 5 5.2 Operators in C
• Example: Assume, int x = 4, y = 5, z = 8;
T
T
T
(z >= y) && (x != 3)
Expression1 Expression2 Expression1 &&
Expression2 Sample Expression
F
F
F
(y > 10) && (z <= x)
F
T
F
(z <= y) && (x == 4)
T
F
F
(y != z) && (z < x)
3/5/2019
4
13
Chapter 5 5.2 Operators in C
Increment / Decrement operators
Operator
Called
Sample
Expression
Similar
Expression
Explanation
++
preincrement
++a
a = a +1
a += 1
Increment a by 1, then use
the new value of a in
expression in which a
reside
++
postincrement
a++
a = a +1
a += 1
Use the current value of a
in the expression which a
reside, then increment a by
1
--
predecrement
--a
a = a -1
a -= 1
Decrement a by 1, then
use the new value of a in
expression in which a
reside
--
postdecrement
a--
a = a -1
a -= 1
Use the current value of a
in the expression which a
reside, then decrement a
by 1
14
Chapter 5 5.2 Operators in C
Operators precedence
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
• Parentheses are used in C expressions in the same
manner as in algebraic expressions.
• For example, to multiply a times the quantity b + c,
we write a * ( b + c ).
• C applies the operators in arithmetic expressions in a
precise sequence determined by the rules of operator
precedence, which are generally the same as those in
algebra.
15
Chapter 5 5.2 Operators in C
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
• The rules of operator precedence specify the order C uses
to evaluate expressions from left to right. • Summarized rules of operator precedence for the operators:
16
Operators
Associative
( )
Left to right
++ -- + - !
Right to left
* / %
Left to right
+ -
Left to right
< <= > >=
Left to right
= = !=
Left to right
&&
Left to right
| |
Left to right
= *= += - =
/= %=
Right to left
Chapter 5 5.2 Operators in C
3/5/2019
5
17
Chapter 5 5.2 Operators in C
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
• Example : Evaluation of the operator precedence rules
y = a * x * x + b * x + c;
• The circled numbers under the statement indicate the order
in which C performs the operations
• Suppose variables a, b, c and x in the preceding second-
degree polynomial are initialized as follows: a = 2 b = 3 c = 7 x = 5
18
Chapter 5 5.2 Operators in C
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
19
Example 1:
int a=10, b=20, c=15, d=8;
a*b/(-c*31%13)*d
1. a*b/(-15*31%13)*d
2. a*b/(-465%13)*d
3. a*b/(-10)*d
4. 200/(-10)*d
5. -20*d
6. -160
Chapter 5 5.2 Operators in C
Example 2:
int a=15, b=6, c=5, d=4;
d *= ++b – a/3 + c
1. d *= ++b – a/3+ c
2. d*=7- a/3+c
3. d*=7- 5+c
4. d*=2 + c
5. d*= 7
6. d = d*7
7. d = 28
21
Grouping with Parentheses
3/5/2019
6
22
example Given the declaration,
int a = 9, b = 2, c = 6, d = 10;
and the statement,
a * b / c + 4 > d != 3 * c % a;
Solve the above statement based on the operator
precedence. Show your steps.
SOLUTION: step operator
0 9 * 2/6+4>10 != 3 * 6 % 9
1 * 18 /6+4>10 != 3 * 6 % 9
2 / 3 +4>10 != 3 * 6 % 9
3 * 3 +4>10 != 18 % 9
4 % 3 +4>10 != 0
5 + 7 >10 != 0
6 > 0 != 0
7 != 0 23
Chapter 5 5.2 Operators in C
Sample algebraic and C Expressions
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
• Example: The following expression calculates the arithmetic
mean (average) of five terms.
• The parentheses are required to group the additions
because division has higher precedence than addition. • The entire quantity ( a + b + c + d + e ) should be
divided by 5.
• If the parentheses are erroneously omitted, we obtain a + b + c + d + e / 5, which evaluates incorrectly
as:
24
Chapter 5 5.2 Operators in C
_____________________________________________________________________________________________________________________________________
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
• Example: The following expression is the equation of a
straight line:
• No parentheses are required because the multiplication
evaluated first as its precedence higher than addition.
25
Chapter 5
Write the formula in C statement.
a) .
b) .
c) .
Activity 5.3:
5.2 Operators in C
a+ b
c+ d
1
1+ x2
b2 - 4ac
3/5/2019
7
26
a) (b*b)-(4*a*c)
b) (a+b)/(c+d)
c) 1/(1+(x*x))
Solution 5.3:
Chapter 5 5.2 Operators in C
27
5.1 Introduction
5.2 Operators in C
5.3 Mathematical Expressions
• Introduction
• Multiple assignment
• Combined assignment
5.4 Data Type Conversion
5.5 Mathematical Library Function
5.6 Summary
Chapter 5 Contents
28
• We can create complex expressions using multiple
mathematical operators.
• An expression can be a literal, a variable, or a mathematical
combination of constants and variables.
• Mathematical expression can be used in assignment, printf, other statements:
• Examples:
Chapter 5 5.3 Mathematical Expressions
Introduction
area = 2 * PI * radius;
printf (“Border is: %d", 2*(l+w));
29
Chapter 5 5.3 Mathematical Expressions
• Some types of mathematical expressions:
Expressions
Primary Binary Assignment Postfix Prefix
3/5/2019
8
30
Chapter 5 5.3 Mathematical Expressions
Primary expression
31
Chapter 5 5.3 Mathematical Expressions
Binary expression
32
Chapter 5 5.3 Mathematical Expressions
Assignment expression
33
Chapter 5 5.3 Mathematical Expressions
Prefix expression
3/5/2019
9
34
Chapter 5 5.3 Mathematical Expressions
Postfix expression
35
• The ‘=‘ can be used to assign a value to multiple variables:
x = y = z = 5;
• Value of ‘=‘ is the value that is assigned.
• Associates right to left: x = (y = (z = 5));
Chapter 5 5.3 Mathematical Expressions
Multiple assignment
Value is 5
36
• Look at the following statement:
sum = sum + 1;
• This will adds 1 to the variable sum.
• Examples:
Chapter 5 5.3 Mathematical Expressions
37
• The combined assignment operators provide a shorthand for
these types of statements.
• The statement: sum = sum + 1;
is equivalent to sum += 1;
Chapter 5 5.3 Mathematical Expressions
Combined assignment
Table: Combined assignment operators
3/5/2019
10
38
Chapter 5 5.3 Mathematical Expressions
• For increment and decrement operators: ++, --
• Instead of a = a + 1 you can write a++ or ++a
• Instead of a = a - 1 you can write a-- or --a
• What is the difference?
num = 10;
ans = num++;
Post-increment Pre-increment
num = 10;
ans = ++num;
First assign num to ans,
then increment num.
In the end, num is 11
ans is 10
First increment num,
then assign num to ans.
In the end, num is 11
ans is 11 39
5.1 Introduction
5.2 Operators in C
5.3 Mathematical Expressions
5.4 Data Type Conversion
• Type casting
• Named constant
5.5 Mathematical Library Function
5.6 Summary
Chapter 5 Contents
40
• Operations are performed between operands of the same type.
• If not of the same type, C will convert one to be the type of the
other.
• The type of the result depends on the types of the operands.
• If the types of the operands differ (e.g. an integer added to a
floating point number), one is "promoted" to other.
o The "smaller" type is promoted to the "larger" one. char int float double
• This can impact the results of calculations.
Chapter 5 5.4 Data Type Conversion
41
Chapter 5 5.4 Data Type Conversion
int and double
• If all operands are integer, the output will be integer,
otherwise, the output will be double
• Example:
//Program_05.c
main() {
int x = 3, y = 2, output1, output2;
double d = 2.0, output3, output4;
output1 = x / y;
output2 = x / d;
output3 = x / y;
output4 = x / d; }
3/5/2019
11
42
• Used for manual data type conversion
• Useful for floating point division using int
• Format:
(data type)variable
Chapter 5 5.4 Data Type Conversion
Type Casting
43
• Example 1: Get the remainder value
Chapter 5 5.4 Data Type Conversion
_____________________________________________________________________________________________________________________________________
Program_06.c: Compiled with Xcode, Apple Machine
int main( void )
{
double x = 3.0, y = 2.0, output;
output = x % y; // syntax error!!!
printf( "\n Output = %f\n\n", output);
}
44
• Solution 1:
Chapter 5 5.4 Data Type Conversion
_____________________________________________________________________________________________________________________________________
Program_06.c: Compiled with Xcode, Apple Machine
int main( void )
{
double x = 3.0, y = 2.0, output;
output = (int)x % (int)y; // free error
printf( "\n Output = %f\n\n", output);
}
45
• Example 2: Display the average marks
Chapter 5 5.4 Data Type Conversion
_____________________________________________________________________________________________________________________________________
Program_06.c: Compiled with Xcode, Apple Machine
int main( void )
{
int total_marks = 456, num_studs = 5;
double ave_marks1, ave_marks2;
ave_marks1 = total_marks/num_studs;
ave_marks2 = total_marks / num_studs;
printf( "\n ave_marks1 = %f\n\n", ave_marks1);
printf( " ave_marks2 = %f\n\n", ave_marks2);
}
Incorrect result !!!
3/5/2019
12
46
• Solution 2:
Chapter 5 5.4 Data Type Conversion
_____________________________________________________________________________________________________________________________________
Program_06.c: Compiled with Xcode, Apple Machine
int main( void )
{
int total_marks = 456, num_studs = 5;
double ave_marks1, ave_marks2;
ave_marks1 = total_marks/num_studs;
ave_marks2 = (double) total_marks / num_studs;
printf( "\n ave_marks1 = %f\n\n", ave_marks1);
printf( " ave_marks2 = %f\n\n", ave_marks2);
}
47
• Named constant (constant variable): variable whose content
cannot be changed during program execution.
• Used for representing constant values with descriptive names:
• Often named in uppercase letters
Chapter 5 5.4 Data Type Conversion
Named constant
const double TAX_RATE = 0.0675;
const int NUM_STATES = 50;
48
void main()
{
const int num = 5;
num++; // num=num+1
printf(“Value of new num is %d”, num);
}
Chapter 5 5.4 Data Type Conversion
• Example: What will be the output?
• Output:
49
Chapter 5 5.4 Data Type Conversion
#define
• Interpreted by pre-processor rather than compiler
• Does not occupy memory location like const
• Example: Increase the NUM value
#define NUM 5 //no semicolon (;)at end
void main()
{
NUM++; // Error: expression is not
assignable
printf(“Value of new NUM is %d”, NUM);
}
3/5/2019
13
50
Chapter 5 5.4 Data Type Conversion
• Solution: Declare a variable and write an assignment for it.
#define NUM 5
void main()
{
int new;
new = NUM + 1;
printf(“Value of NUM is %d”, new);
}
Value of new NUM is 6
• Output:
51
Chapter 5
Write a program that will convert Malaysian Ringgit
(RM) amounts to Japanese Yen. The conversion
factors to use are:
1 RM = 25.69218 Yen
Solve the problems using constant values.
Display the output as below:
Activity 5.4:
5.4 Data Type Conversion
Tips: .
Refer
Program_04.c
Comment in your
program and save
your file as
Activity_5.4.c
53
5.1 Introduction
5.2 Operators in C
5.3 Mathematical Expressions
5.4 Data Type Conversion
5.5 Mathematical Library Function
5.6 Summary
Chapter 5 Contents
54
Chapter 5 5.5 Mathematical Library Functions
• C mathematical operations are a group of functions in the
standard library of the C programming language implementing
basic mathematical functions.
• The library can be called upon during pre-processing #include
#include <math.h>
#include <stdlib.h>
• The math.h header defines various mathematical functions
and one macro.
• All the functions available in this library take double as an
argument and return double as the result.
3/5/2019
14
55
Chapter 5 5.5 Mathematical Library Functions
Function Library
Function
Example Argument Output
abs(x) stdlib.h x abs(-5)
output 5
int int
exp(x) math.h ex exp(1.0)
output 2.71828
double double
log(x) math.h loge(x) log((2.71828)
output 1.0
double double
pow(x, y) math.h xy
pow(0.16, 0.5)
output 0.4
double,
double
double
sqrt(x) math.h √x and x>=0.0 sqrt(2.25)
output 1.5
double double
Table: Some functions from mathematical library.
56
Chapter 5
Rewrite the following mathematical expressions using
C functions. Assume all variables should be of type
double.
a) .
b) log10(xy)
c) .
Activity 5.5:
5.5 Mathematical Library Functions
_____________________________________________________________________________________________________________________________________
I Hanly, J.R and Koffman, E.B. (2001). C Program Design for Engineers 2/E. United State of America: Addison Wesley Longman Inc. (pg: 100)
u+v ´w2
(x+ v)2
58
5.1 Introduction
5.2 Operators in C
5.3 Mathematical Expressions
5.4 Data Type Conversion
5.5 Mathematical Library Function
5.6 Summary
Chapter 5 Contents
59
Chapter 5 5.6 Summary
• Computation of the data need a specific mathematical
or logical manipulations.
• The arithmetic expressions is operated in a precise
sequence determined by the rules of operator
precedence
• The arithmetic expressions may have multiple and
combined assignment operators.
• Some mathematical operations can be called using in
the standard library function provided in C.
3/5/2019
15
60
Self-Review
Chapter 5
61
Chapter 5
Given the declaration,
int a = 9, b = 2, c = 6, d = 10;
and the statement,
a * b / c + 4 > d != 3 * ++c >= a;
Solve the above statement based on the operator
precedence. Show your steps.
Activity 5.1:
5.2 Operators in C
63
Chapter 5
Given the declaration, int count = 5, x, y, c, d;
and the following statement: d = (x = 5, y = 10, c = (x * --count - y++));
What will be printed by the printf statement below?
printf ("%d %d %d %d", count, c, y, d);
Activity 5.2:
5.2 Operators in C
65
Chapter 5 Self-Review
Write a C program to calculate and display the power (watts) and
the current (amps) by obtaining an input of voltage from user.
Assume the resistance remain constant at 5 ohms.
Exercise 5.1:
Recommended