Wren lays six index cards in a row across the greenhouse table. Each one shows a water reading.
"This is the readings list," he says. "Each card is an element. Its spot in the row is its index."
Comet picks up a seventh card and writes BIGGEST on it. "This is my helper variable."
Nova hovers above the row. "Would you like a hint?" she asks.
"Start BIGGEST with the first card. Then walk the row and ask one question at every card."
Comet raises an eyebrow. "Which question?"
Nova blinks. "That part is your discovery."
Wren grins and hands Comet a pencil.
You will build Beacon's readings list from index cards.
Then you will trace two list algorithms by hand: one finds the maximum and one finds the minimum.
biggest ← readings[1]
FOR EACH reading IN readings
{
IF (reading > biggest)
{
biggest ← reading
}
}
DISPLAY(biggest) | Index | reading | Is reading greater than biggest? | biggest after this card |
|---|---|---|---|
| 1 | 62 | no | 62 |
| 2 | 58 | no | 62 |
| 3 | 54 | ? | ? |
| 4 | 48 | ? | ? |
| 5 | 70 | ? | ? |
| 6 | 68 | ? | ? |
Copy this table onto paper and finish it with your cards.
| Lab finding | True or false? |
|---|---|
| Finding a maximum needs a helper variable that remembers the biggest so far. | ? |
| The maximum and minimum programs differ only in their comparison. | ? |
| If the first card were the biggest, biggest would never change. | ? |
| Six separate variables would make this program shorter. | ? |
Great lab work, developer. Tomorrow you will search a list and retire Comet's six variables for good.