CS 142L Programming Fundamentals Lab 3 Handout: Theory / Syntax / Example
CS 142L Programming Fundamentals Lab 3 Handout: Theory / Syntax / Example
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
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.
• In C++ prefix always resolves in l-value while postfix does not resolve in l-value
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