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

Consider the problem of finding the position, p, of the last occurrence of a giv

ID: 3549550 • Letter: C

Question

Consider the problem of finding the position, p, of the last occurrence of a given number, x, in an array, A. We thus want a function FindLast(A,n,x) with precondition: x occurs in A[1::n] (thus n >= 1) and with postcondition: returns p with 1 <= p <= n such that A[p] = x and x does not occur in A[p + 1::n].


Prove that your iterative algorithm is correct. This will involve stating a suitable


loop invariant.


Algorithm:


Consider the problem of finding the position, p, of the last occurrence of a given number, x, in an array, A. We thus want a function Find Last(A,n,x) with precondition: x occurs in A[1::n] (thus n >= 1) and with post condition: returns p with 1

Explanation / Answer

Yes, Code is perfectly fine, as it check the number in an array, one by one, & compares to returnVal, and then move to n+1 .


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