Hi Everyone,
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.
pwr
that's good, because i don't know any programming languages. i'd probably do something like this:
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
.......End if
End do
Return largest
Print "The largest term is the " largest "th term"
Slight problem as you access S(n+1) inside the loop you are accessing an
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))
Try:
//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
idx=1
for j from 2 to n
.... if S(j) >=S(idx) then
........ idx = j
.... end_if
end_for
return idx
RonL