I am new to this board and unfortunately though I can usually hold my own with math I seem to be out of my league with this math class. I am being asked:
Write an algorithm that returns the index of the first occurrence of the largest element in a sequence S1,...,Sn
Seems like it should be simple yet I just can't get my head around it.
Thanks for any help you can give.
Define a sequence S(n) = f(n) ..................(f(n) is some function of n)
let largest = 0
For j from 1 to n, do:
.......If S(j) >or= S(j + 1), then largest = j
.............else largest = j + 1
Print "The largest term is the " largest "th term"
undefined element. Also consider what happens if:
S(1..3) = [2,3,4]
since S(j)<S(j+1) the test is never satisfied, and so 0 is returned (ignoring
the fact that we try to access element S(4))
//Assume array S(1,..,n) is non empty, that is n>=1
//index of largest element held in idx, which is returned at the
//end as the answer
for j from 2 to n
.... if S(j) >=S(idx) then
........ idx = j