Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Page 1 of 3

DEPARTMENT OF COMPUTER SCIENCE


Rachna College of Engineering & Technology, Gujranwala
(a constituent college of UET Lahore)

CS 142L Programming Fundamentals Lab 3 Handout

Name: Registration No:


Marks: Evaluation Comments:

Lab objectives:
• In this lab you will learn the use of unary and compound assignment operators
• Have batter understanding of C++ expressions in terms of precedence and associativity
• Understanding reequipments form a scenario and implementing it using C++ language

Theory / Syntax / Example

1 Compound Assignment
• When multiple variables need to assign same value, you can assign them in single statement
rather than assigning them individually in separate statement
• Variable1 = variable2 = variable3 = VALUE or variable;
• age1 = age2 = age3 = age4 = 19;
• maxPhyMarks = maxEngMarks = maxMathMarks = 100;
• Compound assignment is right to left associative, means right most value is populated to
left in sequence.

2 Compound Assignment Operators


• Using compound assignment operators you can perform arithmetic operation on a variable
and store result into same variable
• +=, -=, *=, /=, %=
• For example time /= 60;

3 Unary operators: prefix postfix increment / decrement


• Unary operators require only one operand to work with
• It increases or decreases the variable value by 1
• Unlike other operators, operand of these operators can only be a variable
• ++ is used for increment and -- is used for decrement
• If operator appears on left/before variable then it is prefix, e.g. ++x is prefix increment
• If operator appears on right/after variable then it is postfix, e.g. x-- is postfix decrement
Session BSCS 2021 1st Semester December 06, 2021 Teacher: Shahzad Aslam
Page 2 of 3

• In C++ prefix always resolves in l-value while postfix does not resolve in l-value

4 Precedence and associativity of operators


• If an expression has multiple operators, the operators will be applied in an order. The order
in which they are applied is called precedence of operator. For example * has priority over
+.
• If an expression has operators with same precedence then which operator will be applied
first is called associativity of operator.
• See theory lecture for details.

Lab Tasks
1. Trace the values of the variables after each statement, write the reason if any statement has
error:

int a, b, c;
cout<<”A is: “<<a<<”, b is:”<<b<<”, c is:”<<c<<endl;
a = b = c = 4;
b++;
a +=b;
a = b++ + c;
a = ++ b + c + b++;
b += --c + ++b + c++;
c += --c + ++b + c++;
c += ++c + ++b + c++ + c-- - --c;
a = 2 * ++b – c+4;
c -= b++ +++5 – 3--;
2. *A scientist simulated an experiment that took days to complete. His stopwatch gave him
only large number of seconds. Help him to convert these seconds to days, hours, minutes
and seconds. Use maximum number of compound assignment operators and your program
should not calculate same thing again (save your intermediate calculations in separate
variable as required)
3. Input any number of arbitrary length from user, apply an compound assignment operation
such that this operation guarantees that resulting answer will be between 0 and 50
4. *Figure out what below code is doing and Re-write it without compound assignment
statements, also remove any syntax errors and write it in such a way that it uses less
operations to achieve same results

int x, y, z, r;
cout<<”Enter three values:”;
cin<<”Enter x”>>x<<”Enter y”>>y<<”Enter z”>>z;
r=1;
r++;
r*=x*y;
y*=y
r*=-1;
r+=y;
x*=x;
r+=x;
cout<<”Result is: “<<rx;

Session BSCS 2021 1st Semester December 06, 2021 Teacher: Shahzad Aslam
Page 3 of 3

5. *When a person dies, his property is divided among his wife and children. According to
Islamic Law, from the amount funeral will be performed and rest will be divided. Wife gets
1/8 of the total and remaining will be divided between children; Son will get double than
daughter. Create a program that take amount a person left, and how many son and
daughters he had then calculate and show share of everyone is well formatted way. Use
many compound assignment operators as you can.
6. The below statements are so ambiguous, re-write them to increase readability such that e
each of the variable should have same value after you new code. Keep in mind precedence
and associativity

int w = 8, x = 4, y = 2, z = 1;
w -= x /= y *= z += 1 + z++ + ++x;

Note: Tasks marked as * are required to be added in lab manual. Use separate A4 pages, tasks
should be hand written. You have to draw flow chart, and write pseducode/steps where asked. Use
proper comments to describe your code where required.

Session BSCS 2021 1st Semester December 06, 2021 Teacher: Shahzad Aslam

You might also like