A program is reversible if it is always possible to undo

0 downloads 0 Views 68KB Size Report
Primary Examination, Semester 1, 2017. (d) A program is reversible if it is always possible to undo the effect of it and restore the original state of its variable(s) ...
COMP SCI 1102, 1102BR

Page 4 of 10 Primary Examination, Semester 1, 2017

(d) A program is reversible if it is always possible to undo the effect of it and restore the original state of its variable(s) before operation was executed. For instance, incrementing a counter by 1: x = x + 1; // x of type int is a reversible command, since you can return to the original state by simply decrementing x’s current value: However, integer division is not reversible. For example, x = x / 2; // if x was 5, it is now 2. // if x was 4, it is now 2. Therefore, integer division is not reversible because 2 different states, 4 and 5, result in the same final state. Analyze the following code fragments and determine if they are reversible: • if your answer is Yes, reversible (i.e. the command is reversible), write a piece of code which restores the original state of the program variables. • If your answer is No (i.e. it is not always possible to undo the effect), provide an example where it is not possible to infer the original state from the outcome. i. int x; if ( x > 10 ) { x = x - 1; }

ii.

int x; if ( x > 10 ) { x = x + 1; } [6 marks] [Total for Question 4: 16 marks]

PLEASE SEE NEXT PAGE

Suggest Documents