Back

Pseudocode Challenge: Arithmetic

Difficulty  

divider

Objective

Rewrite short Lua programs in the notation the AP exam uses. No computer needed — this one works on paper, and doing it on paper is closer to the real thing.

You will never write this notation in a program. You only need to read it fluently, because that is what the multiple-choice section is made of. Translating in this direction is how reading gets easy.

Skills to Practice

  • Reading and writing assignment as an arrow
  • Splitting one print into separate DISPLAY statements
  • Recognizing MOD as the same operation as %

What You Are Allowed to Use

Everything from the exam reference sheet that we have covered so far. Nothing else appears in these problems.

Assignment
a ← expression
Display
DISPLAY(expression)
Arithmetic
a + b a - b
a * b a / b
a MOD b
Random
RANDOM(a, b)

Four rules that catch people:

  • Assignment is , never =. If you are writing by hand, draw the arrow.
  • DISPLAY takes one value. Two things means two statements.
  • There is no local and there are no semicolons. A variable comes into existence the first time you assign to it.
  • There is no .. on the reference sheet. Nothing joins two strings together, which is exactly why one print can become two DISPLAY statements.

Worked Example

Lua
local total = 12 + 8
print(total)
Exam Notation
total ← 12 + 8
DISPLAY(total)

Two lines in, two lines out. Notice what disappeared: local and print.


Translate These

Write the exam-notation version of each program. Line for line where you can.


1

Problem 1
local crewCount = 7
print(crewCount)

2

Careful with the last line. One print that glues a label onto a number with .. does not stay one statement.

Problem 2
local boxes = 47
local perCrate = 6
local leftOver = boxes % perCrate
print("Left over: " .. leftOver)

3

Keep the parentheses. They mean the same thing in both notations.

Problem 3
local a = 9
local b = 4
local c = (a + b) * 2
print("Result: " .. c)

4 — The Other Direction

This one is already in exam notation. Write down exactly what it displays, in order.

Problem 4
x ← 17
y ← 5
DISPLAY(x / y)
DISPLAY(x MOD y)

Both answers are different numbers, and neither one is a whole-number division. Work them out by hand before checking.

Then say where they land on the screen. Lua's print gives you two lines. DISPLAY does not — it puts a space after whatever it shows, so two of them in a row give you one line with a gap in the middle.

Submit

  • Your translations of problems 1, 2, and 3
  • The two values displayed by problem 4, laid out as they appear
  • One sentence: which part of the translation felt least natural, and why?

Worth 3 points, graded on completion. Handwritten and photographed is fine, and is the closest thing to exam conditions.

Commence Challenge