Lesson 56: Trace with the board
Let the board check your trace
- A trace table follows an algorithm by hand: one row each time a value changes.
- In this lesson you write the exam algorithm in C++ and let the board print the rows.
- First fill in the trace table on paper. Then run the program and compare.
Words for this lesson
| Word | Meaning |
|---|---|
| trace table | a table of the values of the variables as the algorithm runs |
| dry run | working through an algorithm by hand |
| rogue value | a value that stops input, like −1 here |
The exam algorithm (0478/22/O/N/24 Q7)
DECLARE Count : INTEGER
DECLARE Answer : INTEGER
DECLARE Value : INTEGER
REPEAT
INPUT Value
IF Value <> -1
THEN
Answer ← Value
FOR Count ← Value - 1 TO 1 STEP -1
Answer ← Answer * Count
NEXT Count
OUTPUT Answer
ENDIF
UNTIL Value = -1
| Pseudocode | C++ |
|---|---|
| REPEAT … UNTIL Value = -1 | do { … } while (value != -1); |
<> |
!= |
| FOR … STEP -1 | for (…; count >= 1; count--) |
In the exam
- 0478/22/O/N/24 Q7: (a) trace table for 5, 6, −1, 20, 9, 4. Mark scheme: OUTPUT 120, then 720; the table stops at −1. (b) The purpose: it multiplies the number by every whole number below it (a factorial).
- 0478/22/M/J/25 Q6: (a) trace table for 2, RACECAR, TREAT: Successful, then NOT successful. (b) Mark scheme: it finds words that read the same forwards and backwards.
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
Turn the exam pseudocode on this page (0478/22/O/N/24 Q7) into C++. do … while is REPEAT … UNTIL. Inside, if the value is not −1: set answer to the value, count down from value − 1 to 1 multiplying answer by count, and print each row as Count 4, Answer 20. Then print OUTPUT and the answer. Check types the exam data: 5, 6, −1, 20, 9, 4. Compare the output with your own trace table.
実行ボタンをクリックして出力を確認してください。
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
The second exam algorithm (0478/22/M/J/25 Q6) checks words. V1 starts at the first letter and V2 at the last. While the letters match and V1 < V2, move V1 forward and V2 back; if two letters are different, stop. Write that while loop. Careful: C++ strings start at 0, so V1 is 0 and V2 is word.length() - 1. Check types 3, then RACECAR, TREAT and NOON.
実行ボタンをクリックして出力を確認してください。