1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Algorithm question - totally lost

  1. Mar 7, 2007 #1
    1. The problem statement, all variables and given/known data

    Write an algorithm that returns the index of the first occurence of the largest element in the sequence s1,.........,sn.

    Example: If the sequence is 6.2 7.9 4.2 8.9
    the algorithm returns the value 4.

    I am new here....I read the rules. I am NOT looking for the answer, just some explanations...pointers, so I may be able to understand it and figure it out. I have read the chapter in my book over and over and I just feel like I am reading a different languauge...which I guess I am. I am so very lost.:redface:

    If anyone would be so kind as to help me out, that would be greatly appreciated.

    Last edited: Mar 7, 2007
  2. jcsd
  3. Mar 7, 2007 #2


    User Avatar

    Staff: Mentor

    What computer language are you supposed to write the code in?

    To do this, use a variable to count the indexes. Call it "i", and start it at one and increment it for each new index (number) that you scan and check.

    Next, you will want to keep a variable that would be the largest value that you have found so far, call it value_max. Of course, for i=1, that's the first number you have scanned, so it automatically becomes the largest so far, right? You will also need a variable, call it i_max, where you keep track of the index of the largest number that you find overall.

    So in words, the algorithm would be something like this:

    -- If the input list is empty, throw an error and halt.

    -- If the input list is not empty, proceed.

    -- i=1, i_max=1, value_max=List(i).

    -- Now if the list has more than one entry, you will do a loop until the end of the list. In that loop, you will first increment i, then check to see if the List(i) value is bigger than the value_max that you already have stored. If it is, replace the value in value_max with the new bigger value, and replace i_max with the current value of i.

    -- When you reach the end of the list, print out the index of the max value.

    Does that help? It's the way many scanning-type algorithms will be done. Now show us the above algorithm in whatever computer language or pseudo-code you are supposed to use, and we'll see if it looks right.
  4. Mar 7, 2007 #3

    Does this make sense?

    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
  5. Mar 7, 2007 #4


    User Avatar

    Staff: Mentor

    Yeah, that's basically it. It's generally good programming practice to be able to react appropriately if given an empty list, so that's why I mentioned that step first in my explanation. But on simple algorithm exercises in your class, you may or may not be expected to do that. (But you might get extra credit for throwing stuff like that in....)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook