Algorithm question - totally lost

  • Thread starter Thread starter bretta
  • Start date Start date
  • Tags Tags
    Algorithm Lost
Click For Summary
The discussion revolves around creating an algorithm to find the index of the first occurrence of the largest element in a sequence. A user expresses confusion about the task and seeks clarification, indicating they have difficulty understanding the material. A response outlines a step-by-step approach to the algorithm, emphasizing the use of variables to track the largest value and its index while iterating through the list. It also suggests handling edge cases, such as empty lists, to enhance programming practices. The explanation concludes with a prompt for the user to implement the algorithm in their chosen programming language for further review.
bretta
Messages
2
Reaction score
0

Homework Statement



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.

Bretta
 
Last edited:
Physics news on Phys.org
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.
 
THANK YOU...THANK YOU...THANK YOU

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
}
 
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...)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K