← Back to course
Intro to CS 9-10 / Week 05 / Wednesday
3/6
Week 05 · Lists

Wednesday

Beacon Lab: A list in your hands
// Keeping track of many values at once
⏱ about 20 min

Wednesday: Beacon Lab: A List in Your Hands

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.

Your mission

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.

  • Eight index cards or squares of paper
  • A pencil
  • Sticky notes or scrap paper for index numbers
  • A partner, if you have one, to check your trace
  1. Write one reading on each of six cards: 62, 58, 54, 48, 70, 68.
  2. Lay them in a row in that order. Put an index note from 1 to 6 above each card.
  3. Label a seventh card biggest. Copy the first reading, 62, onto it.
  4. Walk the row from index 1 to 6. At each card, compare it with biggest.
  5. If the card is greater than biggest, cross out biggest and write the card's value.
  6. Fill in the trace table as you go.
  7. Label an eighth card smallest and repeat the walk to find the minimum.
biggest ← readings[1]
FOR EACH reading IN readings
{
  IF (reading > biggest)
  {
    biggest ← reading
  }
}
DISPLAY(biggest)
IndexreadingIs reading greater than biggest?biggest after this card
162no62
258no62
354??
448??
570??
668??

Copy this table onto paper and finish it with your cards.

PREDICT, THEN CHECK
  • Read the question.
  • Tap your answer.
Before you walk the row, predict: at which index will biggest change?
What does the program display?
Card 6 is 68. Why does biggest stay 70?
NOW FIND THE MINIMUM
  • Read the question.
  • Tap your answer.
For the minimum, which comparison belongs in the IF?
What is the minimum reading?
How many times did smallest change after it started at 62?
Lab findingTrue 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.?
WHY THIS EXERCISEMinimum and maximum are among the most common list algorithms, and both depend on a running helper value.
On paper, sketch your six cards and your biggest card, with arrows showing each time biggest changed.

Great lab work, developer. Tomorrow you will search a list and retire Comet's six variables for good.

← Tuesday