1. Assume the following rules of associativity and precedence for expressions

Precedence:

Highest
*, /, not
+, -
- (unary minus)
=, /=, <, <=, >= , >
and
or, xor
Lowest

Associativity Left to right

Show the order of evaluation of the following expressions by parenthesizing all subexpressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression

a + b * c + d

the order of evaluation would be represented as

((a + ( b * c )1)2 + d)3
  • s / t +v * t - v
  • (t - s) /v * t
  • (a > b) or (not true and false)

2.

Let the function FUN be defined as

int FUN ( int & T )
{
T =T + 2;
return T;
}

Suppose FUN is used in a program as follows

I = 200;
sum1 = FUN( I ) + I * 2;
J = 10;
sum2 = ( J / 2 ) + FUN( J );

What are the values of sum1 and sum2

  • if the operands in the expression are evaluated left to right?
  • if the operands in the expression are evaluated right to left?

3. Rewrite the following code segment using a loop structure in C/C++

A = T + 2;
Loop:
B = A * 3;
B++;
If (B < 100) goto Loop;
T = T + A;

4. Suppose that C++ did not have short circuiting in Boolean expressions. Rewrite the following code to avoid ever dividing by zero:

cin >> Z >> X;
if ( Z != 0 && X / Z < 3 )
cout << Z;

cout << X;
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.