Lab2 QP

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Lab Test 2

19CSE205: Program Reasoning

Department of Computer Science and Engineering

Amrita School of Engineering, Coimbatore

Duration : 1 hour COs: CO1, CO2, CO4

Instructions:

1. All answers require some explanation.


2. For Alt-Ergo code you can show screenshot of code with output.
3. Create a word document of answers (please follow the question order, right question
number). Each answer should include the screenshot of the written part followed by Alt-
Ergo screen shot.
4. Accepted formats: .docx or .pdf. Other formats not accepted.
5. Document should include the exam header, and your name and roll number.
6. The submission is due at 3.00pm and viva/evaluation will start immediately.

[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)

(c)Verify the correctness of the program in alt-ergo. (2 marks)

2. Given the code snippet: [CO2,CO4][DL3]][10 marks}

x=a

y=b

total = 0

while (x > 0){


if(x%2 = 1)

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)

(d) Verify the start VC, Intra-loop and Exit VC in alt-ergo. (2


marks)

You might also like