Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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 postcondition (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 precondition of the code.

image text in transcribed

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 > 0 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) { t = 0; i = 0; /* Loop invariant: A[t] is the largest element of A[O,..,t] */ while (i A[i+1]) { 7. swap = A[i]; A[i] = A[i+1]; 9. A[i+1] = swap; t = i+1; 5. wh 10. i++; 7 12. bound = t; Postcondition: A[0] A[n-1] 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 > 0 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) { t = 0; i = 0; /* Loop invariant: A[t] is the largest element of A[O,..,t] */ while (i A[i+1]) { 7. swap = A[i]; A[i] = A[i+1]; 9. A[i+1] = swap; t = i+1; 5. wh 10. i++; 7 12. bound = t; Postcondition: A[0] A[n-1]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

What is your favorite childhood memory?

Answered: 1 week ago