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

Given an array A of numbers of length n (indexed starting at 0), verify that the

ID: 3143180 • Letter: G

Question

Given an array A of numbers of length n (indexed starting at 0), verify that the following pseudocode program returns the index of the smallest element in the array.

Answer should have loop invariant, initialization, maintenance and termination explanation!

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 < n, i++) {

if (A[i] < minVal) {

minVal = A[i]

minLoc = i

}

}

return minLoc

Explanation / Answer

Yes '

it returns the correct answer

because here we start with assumption that first element is small and we go on checking with all the elements in the array and if we find a smaller element the we change the value of minValue and minLoc

Hence at last minValue gives us the smallest value in the given array and minLoc gives its location

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote