Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(30 points) Invariants Let A and B be two one-dimensional arrays of length n of

ID: 3756356 • Letter: #

Question

(30 points) Invariants Let A and B be two one-dimensional arrays of length n of some value type. The following algorithm implements an operation that swaps each of the elements of A with the corresponding element of B.

for i 0 to n 1 do

t B[i]

B[i] A[i]

A[i] t

Give an invariant for the swap operation that captures the work done. Do not use e.g. true as your invariant. Show that your invariant is indeed an invariant.

. (30 points) Invariants Let A and B be two one-dimensional arrays of length n of some value type. The following al gorithm implements an operation that swaps each of the elements of A with the corresponding element of B. tor i 0 to n _ 1 do Give an invariant for the swap operation that captures the work done. Do not use e.g. true as your invariant. Show that your invariant is indeed an invariant.

Explanation / Answer

Answer:

                                                                                                                                                                                                              

            In our case we'll be using the subarrays A[0....i-1] and B[0....i-1] as the loop invariants.

Step 1- Initialization :-

Step 2- Maintenance :-

Step 3- Termination :-

      Thus the loop invariants used by us are correct as they verify the correctness of the above algorithm.