Question: Problem 4: You have the following code on which the programmer included desired precondition and postconditions and included (hopefully) correct loop invariants on each of

Problem 4: You have the following code on which the programmer included desired precondition and postconditions and included (hopefully) correct loop invariants on each of the loops. Given the stated post- condition (i.e. goal) for the code, you are to: a) State whether the given loop invariants are correct, and if not to give corrected loop invariants. b) For each numbered statement, give the correct weakest precondition for that statement. c) Determine whether the weakest precondition for statement 1 is logically inferred from the stated precon- dition of the code. Precondition: n >O and A contains n elements indexed from 0 1. bound = n; /* Loop invariant: A[bound,..,(n-1)] is sorted (non-decreasing) and A[bound] >= A[O,.. , bound-1] */ 2. while (bound > 0) { 3. t = 0; 4. i = 0; /* Loop invariant: A[t] is the largest element of A[0,..,t] */ 5. while (i A[i+1]) { 7. swap = A[i]; 8. A[i] = A[i+1]; 9 A[i+1] = swap; 10. t = i+1; } 11. i++; } bound = t; } Postcondition: A[0] O and A contains n elements indexed from 0 1. bound = n; /* Loop invariant: A[bound,..,(n-1)] is sorted (non-decreasing) and A[bound] >= A[O,.. , bound-1] */ 2. while (bound > 0) { 3. t = 0; 4. i = 0; /* Loop invariant: A[t] is the largest element of A[0,..,t] */ 5. while (i A[i+1]) { 7. swap = A[i]; 8. A[i] = A[i+1]; 9 A[i+1] = swap; 10. t = i+1; } 11. i++; } bound = t; } Postcondition: A[0]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
