Back to Challenge Set 17

Pseudocode Challenge: Tracing Procedures

Difficulty  

divider

Objective

Three traces in AP exam notation, all about what a procedure hands back — the thing the multiple-choice section asks about most often once procedures appear.

Work these on paper. There is no computer on exam day.


Reference Card

What you are allowed to use
PROCEDURE procName(p1, p2) define a procedure
{
...
RETURN(expression) hand a value back AND leave immediately
}
result ← procName(a, b) call it and keep what it returns
procName(a, b) call it and ignore what it returns
DISPLAY(expression) show it, FOLLOWED BY A SPACE
= is equality. ← is assignment. There is no ==.
IF(condition) { } ELSE { } the only two selection forms

FOR EACH s IN scores visits every element in order, the way Activity 1.25's exam panel showed. C# has a version too, but this course has not taught it yet; on the exam you only need to read it.


The Problems

Problem 1 — Trace

What does this display?

Problem 1
PROCEDURE addTax(price)
{
RETURN(price + price * 0.1)
}
DISPLAY(addTax(100))
DISPLAY(addTax(addTax(100)))

Careful: the second line calls addTax twice. Work out the inner call first, then use its answer as the outer call's argument.

Problem 2 — Trace

Build a table of s and what is displayed on each pass.

Problem 2
PROCEDURE band(n)
{
IF(n ≥ 90)
{
RETURN("A")
}
ELSE
{
IF(n ≥ 80)
{
RETURN("B")
}
ELSE
{
RETURN("C")
}
}
}
scores ← [95, 80, 79]
FOR EACH s IN scores
{
DISPLAY(band(s))
}

Then answer this: 80 sits exactly on a boundary. Which branch takes it, and how do you know from the code rather than from guessing?

Problem 3 — Trace

What does this display?

Problem 3
PROCEDURE mystery(a, b)
{
IF(a > b)
{
RETURN(a)
}
RETURN(b)
}
DISPLAY(mystery(3, 9))
DISPLAY(mystery(9, 3))
DISPLAY(mystery(4, 4))

Then answer this: there is no ELSE here, and the procedure still always returns exactly one value. Explain why.


Submit

  • What Problem 1 displays, with the value of each call written beside it
  • Your Problem 2 trace table and your boundary answer
  • What Problem 3 displays, and your answer about the missing ELSE

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

Commence Challenge