0% found this document useful (0 votes)
41 views

CSC126 Tutorial 4

The document provides an overview of arithmetic operators and order of precedence in C++. It discusses unary operators like increment/decrement that require one operand, and binary operators like addition/multiplication that require two operands. It also describes how C++ uses predefined math functions like sqrt, abs, pow to calculate square roots, absolute values, and exponents instead of defining its own operators for these operations. The document then presents sample tasks to trace output statements using operators and to evaluate expressions involving arithmetic, relational and logical operators.

Uploaded by

maya adam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

CSC126 Tutorial 4

The document provides an overview of arithmetic operators and order of precedence in C++. It discusses unary operators like increment/decrement that require one operand, and binary operators like addition/multiplication that require two operands. It also describes how C++ uses predefined math functions like sqrt, abs, pow to calculate square roots, absolute values, and exponents instead of defining its own operators for these operations. The document then presents sample tasks to trace output statements using operators and to evaluate expressions involving arithmetic, relational and logical operators.

Uploaded by

maya adam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 4: Arithmetic Expressions

Objectives:

1. To use arithmetic operators, multiple operators and additional operators.


2. The precedence of arithmetic operators.

Arithmetic Operators

• C++ operators:
o ‘ + ’ = addition
o ‘ - ’ = subtraction
o ‘ * ’ = multiplication
o ‘ / ’ = division
o ‘ % ’ = remainder (mod operator)
• Order of precedence:
o Operations inside () are evaluated first
o *, /, and % are at the same level of precedence and are evaluated next
o + and - and have the same level of precedence and are evaluated last
o When operators are on the same level, performed from left to right

Multiple Operators

• Unary operators:
o are those that require only one operand
o For example: ++num;
• Binary operators:
o operator that operates on two operands
o For example: x * y;

Additional operators:
• C++ does not define operators for finding square roots, absolute values, or logarithms, or for
raising numbers to a power
• It provides predefined program units called functions that do these and other necessary
calculations.
• Example:
o √𝑥 = sqrt (x)
o √𝑎 − 𝑏 = sqrt (a-b)
o |x| = abs (x)
o |y| = fabs (y)
o |a*r| = fabs (a * r)
o xy = pow (x, y)


Task 1: Trace the output of following statements

int a = 0, b = 1, c = 2;

a--; b++; c--;


++a; --b; ++c;
a--; b++; c--;

cout << a + 2 << endl;


cout << --b << endl;
cout << c-- << endl;

Task 2: Given the declarations below, find the results of each expression.

int a=3, b=10, c=7;

double w= 12, y = 3.2;

a) a+b*c
b) a-b-c
c) a/b
d) b/a
e) a-b/c
f) w/y
g) y/w
h) a+w/b
i) a%b/y
j) b%a

Task 3: Given the declarations below, find the results of each expression

double x = 14.5;
double y = 11.0;
double b, z;
int n = 9;
int m = 5;
int i, j, k;

a) z = x - n / m;
b) b=x*y+m/n*2;
c) i = int (x + 0.5) ;
d) j=x*m-n%m;
e) k = m / -n ;
Task 4: Write a C++ assignment statement for each of the following mathematic expression.

a. 𝑍 = 𝑋𝑌 + 2𝑀!

"
b. 𝑎 = .|𝑏 + 𝑐| + #

c. 𝐾 = (𝐿 + 𝑀$ + 5𝑅)/3𝐹𝐶 #

#%&'
d. 𝑎 = % ! &'

#
e. 𝑏 = ;𝑥 −
$'

f. 𝑎 = 2𝑥 ($'&))

g. 𝑏 = 5.(𝑥 + 𝑦)

You might also like