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:
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 .
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.