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
View worked solution
Answer: B
The given pseudocode implements the standard integer division algorithm to divide a positive integer by another positive integer , where represents the quotient and represents the remainder.
1. Identifying the Loop Invariant
Before entering the loop, we initialize:
At this initial state, the relation holds because:
In each iteration of the loop, the variables are updated as follows:
Let's check if the relation still holds after these updates:
Since the relation remains unchanged, is a loop invariant.
2. Termination Condition
The loop runs as long as . Therefore, when the loop terminates, the loop condition must become false:
3. Post-Condition
Upon termination, both the loop invariant and the termination condition must hold simultaneously. Thus, the post-condition is:
This matches Option B.
Free practice · Your answers on this page are not saved.
