1. ## writing algorithms

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.

pwr

2. Originally Posted by pwr_hngry
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.

pwr
is this for a computer class or math class? is a particular programming language required?

3. Sorry probably should have been more descriptive. It is for a Discrete Mathematics class. No particular programming language is required. Thanks again for the help.

pwr

4. Originally Posted by pwr_hngry
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.

Sorry probably should have been more descriptive. It is for a Discrete Mathematics class. No particular programming language is required. Thanks again for the help.
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"

5. Hmm... Seems simple when you put it like that

Thanks for your help! I am sure I will be talking to you again very soon.

pwr

6. Originally Posted by pwr_hngry
Hmm... Seems simple when you put it like that

Thanks for your help! I am sure I will be talking to you again very soon.

pwr
well, that's what i'm here for

7. Originally Posted by Jhevon
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

idx=1

for j from 2 to n
.... if S(j) >=S(idx) then
........ idx = j
.... end_if
end_for

return idx

RonL

8. Originally Posted by pwr_hngry
Hmm... Seems simple when you put it like that

Thanks for your help! I am sure I will be talking to you again very soon.

pwr
Simple but wrong! Try tracing what happens if S(1,..3) = [1,2,3].

RonL

9. Originally Posted by CaptainBlack
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))
ah yes, i see