← Back to course
Intro to CS 9-10 / Week 02 / Wednesday
3/6
Week 02 · Algorithms in Pseudocode

Wednesday

Beacon Lab: Trace the sensor routine
// Exact steps, variables and expressions
⏱ about 20 min

Wednesday: Beacon Lab: Trace the Sensor Routine

Comet slaps a page onto the workbench. "Beacon's first real routine! Five lines. It shows 30. I checked it in my head."

Wren reads it slowly, one finger on each line. "What do you notice about line three?"

"It just adds some water," Comet says. "No big deal."

Nova hovers over the page and lights up a grid. "Let's debug together," she says.

"One column for each variable, one row for each line. Write down what changes, and only what changes."

Comet hands you a pad of sticky notes. "Fine. Developer, prove me right."

Your mission

Hand tracing means running a program yourself, on paper, one line at a time.

You will trace Comet's routine in a trace table and find out what it really displays.

A trace table has one row for each line and one column for each variable.

  • A sheet of paper and a pencil
  • A ruler for drawing the table
  • Two sticky notes, labeled waterLevel and lightLevel
  • This page, with Comet's routine below
waterLevel ← 40
lightLevel ← 70
waterLevel ← waterLevel + 15
lightLevel ← lightLevel - waterLevel
DISPLAY(lightLevel)
PREDICT BEFORE YOU TRACE
  • Read the question.
  • Tap your answer.
Comet says the routine displays 30. Do you predict she is right?
  1. Rule a table with four columns: Line, waterLevel, lightLevel and Screen shows.
  2. Add five rows, one for each line of the routine.
  3. Run line one. Write the new value of waterLevel on its sticky note and in the table.
  4. Run each next line. Only change the sticky note for the variable on the left of the arrow.
  5. On the right side of an arrow, use the value on the sticky note right now.
  6. When you reach DISPLAY, write what the screen shows.
  7. Compare your final answer with Comet's 30.
LinewaterLevellightLevelScreen shows
1
2
3
4
5

Copy this trace table onto paper and fill it in. Then check your values with the questions below.

CHECK YOUR TRACE
  • Read the question.
  • Tap your answer.
After line three, what does waterLevel hold?
After line four, what does lightLevel hold?
What does line five display?
Why did Comet get 30?
Which variable changes on line three?
Running a program yourself on paper, line by line, is called hand ____.
What the lab showsTrue or false?
Line four uses the value waterLevel holds after line three.?
Line two changes waterLevel.?
DISPLAY changes the value of a variable.?
A trace table can catch a mistake that checking in your head missed.?
WHY THIS EXERCISESequencing means every line uses the values left by the lines before it.
HOW TO TRACE ONE ASSIGNMENT LINE
  • Tap a card.
  • Then tap its spot.
1First
2Second
3Third
4Last
On paper, draw the two sticky notes after each line. Make five small pictures in a row, one per line.

Great lab work. Tomorrow you build expressions that do math with Beacon's readings.

← Tuesday