Back to Challenge Set 14

Pseudocode Challenge: Walking a List

Difficulty  

divider

Objective

Three problems on getting through a list one element at a time — Activity 1.25, in AP exam notation. The exam has no loop like C#'s for, so the counting version has to be built by hand. That hand-built loop is one of the most common patterns on the exam.

Work these on paper. There is no computer on exam day. All three are traces, and two of them ask one more question after the trace.


What You Are Allowed to Use

The reference sheet's lists and loops. Arithmetic, IF, and comparison work the way they did in earlier pseudocode challenges.

Reading a list
aList[i]
LENGTH(aList)
Display
DISPLAY(expression)
Every element, first to last
FOR EACH item IN aList
{
<block of statements>
}
A fixed number of times
REPEAT n TIMES
{
<block of statements>
}

Four rules that catch people:

  • FOR EACH hands you the values, not the positions. Inside the loop, item is 7, then 3, then 9 — never an index. C# has a loop like this too, foreach, and it comes later in the course.
  • REPEAT n TIMES has no counter of its own. If you need a position, you keep it yourself: start it before the loop and add one to it inside.
  • The first element is aList[1], so a hand-kept index starts at 1, not 0.
  • An index below 1 or above the length stops the program, the same as C#.

And DISPLAY puts a space after each value, so several values end up on one line.


Worked Example

Every score, written both ways.

C#
int[] scores = { 7, 3, 9 };
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine(scores[i]);
}
Exam Notation
scores ← [7, 3, 9]
i ← 1
REPEAT LENGTH(scores) TIMES
{
DISPLAY(scores[i])
i ← i + 1
}

Three things the C# for line does in one place, the exam version does in three. int i = 0 becomes i ← 1 before the loop, because the exam counts from 1. i < scores.Length becomes REPEAT LENGTH(scores) TIMES. And i++ becomes i ← i + 1 as the last line inside the loop. Both show 7, 3, and 9: the C# on three lines, the exam version on one, as 7 3 9.


The Problems

1 — Trace

What does this display?

Problem 1
scores ← [7, 3, 9]
FOR EACH s IN scores
{
DISPLAY(s)
}

2 — Trace

What does this display? Build a small table of i and total as you go, one row per time through the loop.

Problem 2
values ← [4, 8, 15]
total ← 0
i ← 1
REPEAT LENGTH(values) TIMES
{
total ← total + values[i]
i ← i + 1
}
DISPLAY(total)

Then answer this: what goes wrong if i ← i + 1 moves to be the first line inside the loop instead of the last? Trace it again to find out.


3 — Trace

What does this display?

Problem 3
items ← [5, 10, 15, 20]
count ← 0
FOR EACH v IN items
{
IF(v > 8)
{
count ← count + 1
}
}
DISPLAY(count)

Then answer this: rewrite the whole program with REPEAT LENGTH(items) TIMES and a hand-kept index instead of FOR EACH. It must display the same thing.


Submit

  • What Problem 1 displays
  • Problem 2's table, what it displays, and what moving the increment does
  • What Problem 3 displays, and your rewrite
  • One sentence: which problem took you longest, and what slowed you down?

This is graded on completion, not accuracy. Show your working for the traces — a wrong answer with visible reasoning is worth more to both of us than a right one with none.

Commence Challenge