Back to Challenge Set 13

Pseudocode Challenge: Lists

Difficulty  

divider

Objective

Three problems on lists in AP exam notation — Activities 1.23 and 1.24. This is where the two notations disagree most, and both disagreements are on this page: the exam counts from 1, and ← makes a copy where C# shares one array between two names.

Work these on paper. There is no computer on exam day. One is a translation and two are traces.


What You Are Allowed to Use

The reference sheet's list operations. Nothing else appears in these problems.

Making and reading a list
aList ← [value1, value2, value3]
aList[i]
LENGTH(aList)
Copying a list
aList ← bList
Changing a list's length
APPEND(aList, value)
INSERT(aList, i, value)
REMOVE(aList, i)
Display
DISPLAY(expression)

Four rules that catch people:

  • The first element is aList[1]. The last is aList[LENGTH(aList)], with no minus one.
  • aList ← bList makes a copy. Changing one afterward does not change the other — the opposite of C#.
  • INSERT and REMOVE shift everything after them. Write the whole list out again after every line.
  • An index below 1 or above the length stops the program, the same as C#.

Worked Example

The last element, written both ways.

C#
string[] crew = { "Ada", "Bo", "Cy" };
Console.WriteLine(crew[crew.Length - 1]);
Exam Notation
crew ← ["Ada", "Bo", "Cy"]
DISPLAY(crew[LENGTH(crew)])

Both display Cy, and the minus one is the whole difference. C# counts 0, 1, 2, so the last of three is at 2. The exam counts 1, 2, 3, so the last of three is at 3 — which is its length.


The Problems

1 — Translate

Rewrite this C# in exam notation. Two things need to change, and neither is the array's contents.

Problem 1
string[] names = { "Avery", "Blake", "Casey" };
Console.WriteLine(names[0]);
Console.WriteLine(names.Length);

Your version must display the same two things: Avery, then 3.


2 — Trace

Write out the whole list after each line, then give what the program displays.

Problem 2
aList ← [10, 20, 30]
APPEND(aList, 40)
INSERT(aList, 2, 15)
REMOVE(aList, 1)
DISPLAY(LENGTH(aList))
DISPLAY(aList[1])

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


3 — Trace

What does this display?

Problem 3
first ← [1, 2, 3]
second ← first
second[1] ← 99
DISPLAY(first[1])
DISPLAY(second[1])

Then answer this: here is the same program in C#. It displays something different. Say what, and why.

Problem 3, in C#
int[] first = { 1, 2, 3 };
int[] second = first;
second[0] = 99;
Console.WriteLine(first[0]);
Console.WriteLine(second[0]);

Submit

  • Your exam-notation translation of Problem 1
  • The list after every line of Problem 2, and what it displays
  • What Problem 3 displays, and what the C# version displays and why
  • One sentence: which problem took you longest, and what slowed you down?

This is graded on completion, not accuracy.

Commence Challenge