Professional Documents
Culture Documents
Lab2 QP
Lab2 QP
Lab2 QP
Instructions:
[CO2,CO4][DL3]][10 marks}
1. Derive the weakest precondition for the given program code using Loop Invariants, for the post
condition {(x = r + y * q) and (y>r)}
r=x;
q=0;
while(y<=r){
r = r - y;
q = q + 1;
(a) Trace out the program state and identify a loop invariant. (2 marks)
(b) Use intra-loop VC and Exit-VC to check whether the loop invariant is weakest precondition for the
given while loop. Also state the start VC for the pre-condition {x=20,y = 2}. (3 + 2 + 1 marks)
x=a
y=b
total = 0
total = total + y;
x = x-1;
x=x/2;
y = y * 2;
(a) Verify the exit-VC for the loop Invariant Inv = {total + x * y = a * b} to prove correctness of the
above loop for post-condition {total = a*b}. ( 1 marks)
(b) If not valid, strengthen the above loop invariant by adding appropriate conditions. Also verify
the exit and intra-loop VC for the new loop invariant. (3 + 3 marks)
(c) State the start VC for the program for all natural numbers a and b. (1 mark)