Algorithm question - totally lost

  • Thread starter Thread starter bretta
  • Start date Start date
  • Tags Tags
    Algorithm Lost
Click For Summary

Discussion Overview

The discussion revolves around writing an algorithm to find the index of the first occurrence of the largest element in a given sequence. Participants are exploring the problem from a homework perspective, seeking clarification and guidance on how to approach the task without providing direct answers.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Bretta expresses confusion about the algorithm and requests pointers to help understand the problem better.
  • One participant suggests using a variable to count indexes and proposes naming it "i", starting at one, and incrementing it for each index scanned.
  • Another participant introduces the idea of maintaining a variable for the largest value found so far, referred to as "value_max", and a variable "i_max" to track the index of this largest value.
  • A proposed algorithm is shared, outlining steps to handle an empty list and to find the largest element in the sequence.
  • One participant emphasizes the importance of handling empty lists as a good programming practice, noting that it may not always be required for simple exercises.

Areas of Agreement / Disagreement

Participants generally agree on the structure of the algorithm and the importance of handling edge cases, but there is no consensus on whether handling an empty list is required for the homework assignment.

Contextual Notes

There are assumptions regarding the programming language to be used, which have not been clarified. Additionally, the discussion does not resolve whether the algorithm should include error handling for empty lists as a requirement.

Who May Find This Useful

Students seeking assistance with algorithm design, particularly in programming contexts related to finding elements in sequences.

bretta
Messages
2
Reaction score
0

Homework Statement



Write an algorithm that returns the index of the first occurrence 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