Question #34926

Write an algorithm that returns the index of the last occurrence of the value key in a sequence s1, …,sn. If key is not in the sequence, the algorithm returns 0. Example, if the sequence is 12 11 12 23 and key is 12, the algorithm returns 3.
1

Expert's answer

2017-02-23T05:32:38-0500

Input: sequence "s" indexed from 1 to n; number of elements in the sequence "n"

Output: First occurrence of the largest element in the sequence find_largest_element(s,n) {

large = s_1 \\ initializes large

index_large = 1 \\ initializes the index of large

for i = 2 to n \\ steps through the sequence

if (s_i > large) { \\ since this is >, it will not be called if value equals large

large = s_i

index_large = i

}

return index_large

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS