>

All questions on Maths

loop invariant

Given an array A of numbers of length n (indexed starting at 0), verify that the following pseu-docode program returns the index of the smallest element in the array. What is the LOOP invariant Hint: You should have more than one component to your loop invariant, and both minVal and minLoc should appear somewhere in the invariants. def findMin(A): n = length(A) minVal = A[0] minLoc = 0 for (i = 1, i

Answers
Hi Jain,I cannot see your entire post, however, the loop invariant is a condition that holds true after every iteration through your loop. For instance, if you have a function that finds the sum of elements in a list:sum = 0for i in list       sum = sum + list[i]Your loop invariant will be sum = list[0]+...+list[i] and i < length(list) as this will true for every iteration of your code. Does this make sense?
Sarah J.
25 July 2017
>
Add an answer

Similar questions