Week 15 · Unit 7.3: Connections are made · 3 lessons
3
Project: the secret-note coder
Computing 7: Unit 7.3, pp. 82–111
GoalPlan, make, test and evaluate a flowchart that encrypts one letter with a shift of 3.
New words
algorithm · a precise set of steps that solves a problemtest plan · a table of tests that says what should happen, so you can compare it with what does happenboundary test · a test with data at the edge of what is allowed, where mistakes often hideevaluate · to judge how well something works and how it could be better
Explanation
A project has four stages: plan, make, test and evaluate. Our problem is that a reading club wants an algorithm that encrypts one letter with a Caesar shift of 3. Step 1 is to number the alphabet, so A is 1, B is 2 and Z is 26. Step 2 is to add 3 to the number. Step 3 is the tricky part, because X, Y and Z would go past 26, so the algorithm must wrap round by subtracting 26. A decision in the flowchart does this: if the new number is more than 26, subtract 26. Last, the algorithm outputs the letter at the new number. Then we test with letters from the middle and from the end of the alphabet, and compare the expected outcome with the actual outcome.
Worked examples
Step
number
new
Output
after INPUT C
number = place of letter
3
new = number + 3
3
6
new > 26?
3
6
no
OUTPUT
3
6
F
Trace with the letter C. C is number 3, new is 6, and 6 is not more than 26, so there is no wrap. The letter at place 6 is F.
Step
number
new
Output
number = place of letter
25
new = number + 3
25
28
new > 26?
25
28
yes
new = new − 26
25
2
OUTPUT
25
2
B
Trace with the letter Y. Y is number 25, new is 28, and 28 is more than 26, so new becomes 2. The letter at place 2 is B.
Test
Test data
Expected outcome
Actual outcome
Pass/fail
1
A
D
D
pass
2
M
P
P
pass
3
X
A
no letter
fail
4
Z
C
C
pass
A test plan for a faulty copy of the flowchart whose decision says new > 27. The expected outcomes were worked out first. X is the boundary letter: new is 27, which is not more than 27, so there is no wrap and no letter at place 27. Test 3 fails and shows the bug.
Class activity
Human computer (no computer): draw the flowchart on a big sheet. Cut out cards for each box and lay them on the table in order. Write the alphabet with numbers 1 to 26 on a strip. A partner is the user and gives you a letter. You follow the boxes, moving a counter along the strip, and write the output. Test with A, M, X and Z. Then change the shift to 4 and see which letters now need the wrap-round. Write what you would do differently next time.
Practice
1
Trace the letter Z through the flowchart and complete the table.
Step
number
new
Output
number = place of letter
?
new = number + 3
?
?
new > 26?
?
?
?
new = new − 26
?
?
OUTPUT
?
?
?
number = 26. new = 29. 29 is more than 26, so new = 3. The output is the letter at place 3, which is C.
2
Change the algorithm to use a shift of 5. Fill in the missing numbers, then say which letters now need the wrap-round.
The boxes become new = number + 5 and the decision stays new > 26. The letters with number 22 to 26 wrap round: V, W, X, Y and Z become A, B, C, D and E.
3
Write a test plan for the shift-5 algorithm using A, U, V and Z. Work out the expected outcomes first.
Test
Test data
Expected outcome
Actual outcome
Pass/fail
1
A
?
2
U
?
3
V
?
4
Z
?
A: 1 + 5 = 6, so F. U: 21 + 5 = 26, so Z (no wrap). V: 22 + 5 = 27, wrap to 1, so A. Z: 26 + 5 = 31, wrap to 5, so E. V and U are the boundary tests, and they show whether the decision uses 26 correctly.
4
Mia built a flowchart whose decision says new > 25. Her tests are in the table. What is wrong with her flowchart, and how should it be fixed?
Test
Test data
Expected outcome
Actual outcome
Pass/fail
1
X
A
A
pass
2
Y
B
B
pass
3
W
Z
no letter
fail
Test 3 fails: W is number 23, new is 26, and 26 is more than 25, so her flowchart subtracts 26 and gets 0, which is not a letter. The decision should be new > 26, because 26 is the letter Z and does not need to wrap.