Results 1 to 9 of 9

Math Help - writing algorithms

  1. #1
    Newbie
    Joined
    May 2007
    Posts
    23

    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.

    Thanks for any help you can give.
    pwr
    Follow Math Help Forum on Facebook and Google+

  2. #2
    is up to his old tricks again! Jhevon's Avatar
    Joined
    Feb 2007
    From
    New York, USA
    Posts
    11,663
    Thanks
    3
    Quote Originally Posted by pwr_hngry View Post
    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
    is this for a computer class or math class? is a particular programming language required?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    May 2007
    Posts
    23
    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
    Follow Math Help Forum on Facebook and Google+

  4. #4
    is up to his old tricks again! Jhevon's Avatar
    Joined
    Feb 2007
    From
    New York, USA
    Posts
    11,663
    Thanks
    3
    Quote Originally Posted by pwr_hngry View Post
    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"
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    May 2007
    Posts
    23
    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
    Follow Math Help Forum on Facebook and Google+

  6. #6
    is up to his old tricks again! Jhevon's Avatar
    Joined
    Feb 2007
    From
    New York, USA
    Posts
    11,663
    Thanks
    3
    Quote Originally Posted by pwr_hngry View Post
    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
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Jhevon View Post
    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
    Last edited by CaptainBlack; May 3rd 2007 at 02:55 AM.
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by pwr_hngry View Post
    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
    Follow Math Help Forum on Facebook and Google+

  9. #9
    is up to his old tricks again! Jhevon's Avatar
    Joined
    Feb 2007
    From
    New York, USA
    Posts
    11,663
    Thanks
    3
    Quote Originally Posted by CaptainBlack View Post
    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
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Algorithms
    Posted in the Math Software Forum
    Replies: 2
    Last Post: November 4th 2010, 09:25 PM
  2. Algorithms
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: October 8th 2010, 10:48 PM
  3. Algorithms
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: December 17th 2009, 08:11 AM
  4. Algorithms
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: December 4th 2009, 04:06 AM
  5. Algorithms!
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: December 15th 2006, 04:03 AM

Search Tags


/mathhelpforum @mathhelpforum