← Back to course
2/6
Week 05 · Lists

Tuesday

Walking through a list
// Keeping track of many values at once
⏱ about 20 min

Tuesday: Walking Through a List

The greenhouse team asks Beacon for one number: the average water reading for the day.

Comet starts typing readings[1] + readings[2] + readings[3]. She stops. "What if tomorrow has a different number of readings?"

Wren nods. "Then your line breaks. We need a way to visit every element, however many there are."

Comet tries readings[7] to see what happens. The paper test stops with an error.

"There is no seventh box," Wren says.

Nova circles the list on the board. "Let's investigate," she says.

"What if Beacon walked down the list, one box at a time, and kept a running total?"

Indexes have limits

On the AP reference sheet, list indexes run from 1 to the number of elements.

An index less than 1, or greater than the length of the list, produces an error, and the program ends.

IS THE INDEX IN RANGE?
  • Read the question.
  • Tap your answer.
readings has 6 elements. Is readings[7] in range?
Is readings[0] in range?
Is readings[6] in range?

FOR EACH: a traversal

Traversing a list means visiting its elements. A complete traversal visits all of them. A partial traversal visits only some.

FOR EACH item IN aList visits each element in order, from the first to the last.

On each pass, the variable item gets the value of the next element.

total ← 0
FOR EACH reading IN readings
{
  total ← total + reading
}
average ← total / 6
DISPLAY(average)
Passreadingtotal
Start(none yet)0
16262
258120
354174
448?
570?
668?
FINISH THE TRACE
  • Read the question.
  • Tap your answer.
What is total after pass 4?
What is total after pass 6?
What does the program display as the average?
About FOR EACHTrue or false?
FOR EACH visits the elements in order, from first to last.?
On each pass, the loop variable gets the next element's value.?
A complete traversal skips some elements.?
FOR EACH works the same way whether the list has 6 elements or 24.?
WHY THIS EXERCISEA traversal that visits every element is why one short loop can handle a list of any size.
HOW THE AVERAGE PROGRAM RUNS
  • ?After the last reading, divide total by the number of readings.
  • ?Visit the first reading and add it to total.
  • ?Keep visiting readings in order, adding each one.
  • ?Display the average.
  • ?Set total to 0.
WHY THIS EXERCISESum first, then divide: the order of steps is what makes the average correct.
Visiting the elements of a list is called a ____.
A traversal that visits every element is a ____ traversal.
Try it
Write any four numbers on paper as a list. Traverse it with your finger, keeping a running total.
Divide by four to find the average. Check your answer with a partner.

Change one element

You can also assign a new value to one element. The rest of the list stays the same.

In the line below, the element at index 2 gets 60.

readings[2] ← 60
AFTER THE CHANGE
  • Read the question.
  • Tap your answer.
After readings[2] gets 60, what is readings[2]?
After that change, what is readings[3]?
After the change, what is the new total of all six readings?
On paper, sketch the list as six boxes before and after readings[2] gets 60. Circle the box that changed.

Nice work. Tomorrow in Beacon Lab, index cards become a list you can hold in your hands.

← Monday