Back

Written Response Rehearsal: The Algorithm

Four problems · on paper · this is Written Response 2(c)

divider

Objective

The Real Prompt
WRITTEN RESPONSE 2(c)
You are given the procedure isEqual(value1, value2),
which returns true if the two values are equal and
false otherwise.
Using the list from your Personalized Project Reference,
explain in detailed steps an algorithm that counts the
number of elements equal to a certain value.
Your explanation must be detailed enough for someone
else to write the program code.

This is the most predictable question on the exam. It is the same algorithm every year, over whatever list you happen to have put in your Project Reference. You can walk in already knowing how to answer it.

You may write it in code, in exam notation, as numbered steps, or as a paragraph in English. All four are explicitly allowed. What is not allowed is vagueness.

What Earns the Point
The four things the algorithm MUST do:
1 go through EVERY element of the list
2 call isEqual on each element
3 keep a running count
4 produce the count -- store it, return it, or display it
Miss any one and the point is gone.

Paper only.

divider

The Problems


Problem 1 — What You Are Given

isEqual
PROCEDURE isEqual(value1, value2)
{
<this is given to you. You do not write it.>
}
isEqual(4, 4) returns true
isEqual("a", "b") returns false
  1. The exam gives you isEqual. Why does it bother? You could write aList[i] = target instead.
  2. What would isEqual have to do that = might not, if the list held something more complicated than numbers?

Problem 2 — Read a Model

countMatching
colors ← ["red", "blue", "red", "green", "red"]
PROCEDURE countMatching(aList, target)
{
count ← 0
i ← 1
REPEAT UNTIL(i > LENGTH(aList))
{
IF(isEqual(aList[i], target))
{
count ← count + 1
}
i ← i + 1
}
RETURN(count)
}
  1. Point at each of the four required things in the checklist above. Write the line number of each.
  2. What does countMatching(colors, "red") return?
  3. What does countMatching(colors, "yellow") return? Is that a failure?

Problem 3 — Two Answers That Score Zero

Both of these were handed in by someone who understood the problem. Neither earns the point.

Attempt A
Attempt A
count ← 0
i ← 1
REPEAT UNTIL(isEqual(aList[i], target))
{
i ← i + 1
}
RETURN(i)
  1. Name every one of the four requirements this misses.
  2. What does it actually compute?
  3. Describe an input that makes it fail badly, not just return the wrong number.
Attempt B
Attempt B
Go through the list and count how many
match the value, then return the count.
  1. Everything in it is true. Why does it still score nothing?
  2. The prompt says detailed enough for someone else to write the program code. Rewrite Attempt B so it passes that test, staying in English.

Problem 4 — Your Own

Use the list from your own program. Write the full Written Response 2(c) answer: name your list, pick a value, and give the algorithm.

Then check it against the four-item checklist yourself. All four, or it is not finished.

Keep this answer. The version you write today is very close to the version you will write in May — only the list name changes.

divider

Submit

All four problems, plus your own Written Response 2(c) checked against the four-item checklist.

Worth 5 points, graded on completion.

Commence Challenge