Question
Consider the following pseudo code, where and are positive integers:
begin
q := 0
r := x
while r >= y do
begin
r := r - y
q := q + 1
end
end
The post condition that needs to be satisfied after the program terminates is:
Official answer and solution
- Official answer
- B
- From
- Official GATE 2015 answer key
Solution
Determine the loop invariant and termination condition of the standard integer division algorithm.
The algorithm implements integer division of positive integer by positive integer using repeated subtraction:
- Initially: .
At initialization: . - Loop body: at each step, is decremented by and is incremented by :Thus, the loop invariant holds throughout.
- Termination: the while loop
while r >= yterminates when becomes false, i.e., .
Combining the invariant and termination condition:
This matches option B.
B
Switching and in the division formula or postcondition relation.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 1
- Question
- 43 of 65, printed as CS Q.33
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.