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

ersible if it is always possible to undo the effect of it and tate of its variab

ID: 3903039 • Letter: E

Question

ersible if it is always possible to undo the effect of it and tate of its variable (s) before operation was executed. For instance, incrementing a cou nter by 1 x = x + 1; // x of type int s a re decrementing x's current value: ible command, since you can return to the original state by simply However, integer division is not reversible. For x x / 2; // íf x was 5, it is now 2 // if x was 4, it is now 2. ision is not reversible because 2 different states, 4 and Therefore, integer division is not reversible beca 5, result in the same final state. Analyz e the following code fragments and determine if they are reversible: our answer is Yes, reversible (i.e. the command is reversible), write a ece of code which restores the original state of the program yariables., piece our answer is No (i.e. it is not always possible to undo the effect), rovide an example where it is not possible to infer the, original state . If from the outcome, i. int x; if(x >10) xx1 ii. int x; if(x >10) xx1; [6 marks]

Explanation / Answer

i) NO

Consider the following 2 cases :

There is no way to infer the original state when the final state is 10.

ii ) YES

The following code restores the original value of the variable x :

if(x>10)

{

x=x-1;

}