Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) You are given the following C-like code. There are at least three ways to ev

ID: 3830210 • Letter: #

Question

(a) You are given the following C-like code. There are at least three ways to evaluate the expression in statement A that results in variable y taking three different values. Briefly elaborate the three ways and state the value of y after each way of evaluation. (b) Write a function in C that has no functional side effect but is not referential transparent. Your function cannot have any print statements. State the semantics of your function. (c) In C or C++, evaluating the expression a = a++ produces undefined behavior. Give a reason to justify this design.

Explanation / Answer

1.

y=x+foo(&x)+bar(&x); // give y=7 as output

y=foo(&x)+bar(&x)+x;  // give y=9 as output

i have tried all combinations but every time i am getting 7 or 9 as output.

-------------------------------------------------------------------------------------------------------------------------------------------

2.

In this example f performs a side-effect and it is not referentially transparent, while g does not perform a side-effect, but it is non-referentially transparent as well (since the two calls of g(1) produces two different values).

-------------------------------------------------------------------------------------------------------------------------------------------

3.a=a++ produce undefined behaviour in c or c++.

Undefined value means the compiler can give any value. i.e.; different compilers or even different versions of the same compiler can give different answers.

Justification: Just because the ++ comes after the variable does not mean that the increment happens late. The increment can happen as early as the compiler likes as long as the compiler ensures that the original value is used.

Due to this reason, a++ produce undefined behavior.