Question: EXERCISE SET 5.5 Exercises 1-5 contain a while loop and a predicate. In each case show that if the predicate is true before entry to








EXERCISE SET 5.5 Exercises 1-5 contain a while loop and a predicate. In each case show that if the predicate is true before entry to the loop, then it is also true after exit from the loop. 1. loop: while (m > 0 and m = 100) m := m+1 n :=n-1 end while predicate: m +n = 100 2. loop: while (m > 0 and m = 100) m := m+4 n := n -2 end while predicate: m + n is odd 3. loop: while (m > 0 and m = 100) m : = 3.m n : = 5.n end while predicate: m' > n4. loop: while (11 2 0 and n E 100) n := n + 1 end while predicate: 2\" largest then largest := A[i] end while [ Post-condition: largest = maximum value of A[1], A[2], . . ., A[m]]loop invariant: I (n) is "largest = maximum value of A[1], A[2], . . ., A[n + 1] and i = n + 1." 8. [Pre-condition: sum = A[1] and i = 1] while (i # m) 1.i :=i+1 2. sum: = sum + A[i] end while [Post-condition: sum = A[1] + A[2] + . . . + A[m]] loop invariant: I(n) is "i = n +1 and sum = A[1] + A[2] + . . . + A[n + 1]." 9. [ Pre-condition: a = A and A is a positive integer. ] while (a > 0) 1. a : = a -2 end while [ Post-condition: a = 0 if A is even and a = -1 if A is odd. ] loop invariant: I (n) is "Both a and A are even integers or both are odd integers and a 2 -1."
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
