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

CSC126 Tutorial - Lab 4

The document discusses arithmetic expressions in C++. It defines common arithmetic operators like addition, subtraction, multiplication, and division. It also covers the order of precedence for operators and unary/binary operators. Additional math functions like square root, absolute value, and logarithms are provided through predefined C++ functions rather than operators. The document provides example code to trace the output of arithmetic expressions and tasks for writing C++ statements that evaluate various mathematical expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

CSC126 Tutorial - Lab 4

The document discusses arithmetic expressions in C++. It defines common arithmetic operators like addition, subtraction, multiplication, and division. It also covers the order of precedence for operators and unary/binary operators. Additional math functions like square root, absolute value, and logarithms are provided through predefined C++ functions rather than operators. The document provides example code to trace the output of arithmetic expressions and tasks for writing C++ statements that evaluate various mathematical expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab/Tutorial 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. 𝑎𝑎 = �|𝑏𝑏 + 𝑐𝑐| +
2

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

2𝑥𝑥−𝑦𝑦
d. 𝑎𝑎 =
𝑥𝑥 3 −𝑦𝑦

2
e. 𝑏𝑏 = �𝑥𝑥 −
3𝑦𝑦

f. 𝑎𝑎 = 2𝑥𝑥 (3𝑦𝑦−1)

g. 𝑏𝑏 = 5�(𝑥𝑥 + 𝑦𝑦)

Task 5: Write a C++ assignment statement for each of the following mathematic expression.

|𝑎𝑎+𝑏𝑏2 |
a) 𝑉𝑉 =
𝑎𝑎−𝑐𝑐

2𝑎𝑎
b) 𝑊𝑊 = + √𝑐𝑐
𝑏𝑏

𝑎𝑎+𝑏𝑏+𝜋𝜋
c) 𝑋𝑋 =
√𝑑𝑑 3

1
d) Y = 7ab + 2 𝑐𝑐𝑑𝑑4

e) 𝑍𝑍 = √𝑎𝑎𝑏𝑏 3 + 𝑎𝑎𝑏𝑏 2

You might also like