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.
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.
PROCEDURE isEqual(value1, value2){ <this is given to you. You do not write it.>}
isEqual(4, 4) returns true isEqual("a", "b") returns falseisEqual. Why does it bother? You could write aList[i] = target instead.isEqual have to do that = might not, if the list held something more complicated than numbers?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)}countMatching(colors, "red") return?countMatching(colors, "yellow") return? Is that a failure?Both of these were handed in by someone who understood the problem. Neither earns the point.
count ← 0i ← 1
REPEAT UNTIL(isEqual(aList[i], target)){ i ← i + 1}
RETURN(i)Go through the list and count how manymatch the value, then return the count.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.
All four problems, plus your own Written Response 2(c) checked against the four-item checklist.
Worth 5 points, graded on completion.