GoalWork out what an algorithm does, then edit it to change what it does or to add a new task.
New words
edit · to change an algorithm so that it does something different or does an extra jobdry run · following an algorithm by hand, line by line, with test datatest data · values chosen to try an algorithm, including values on the edgesboundary value · a value exactly on the edge of what is allowed, such as the 8 in ‘8 or less’
Explanation
Before you change an algorithm you must find out what it does. Read it line by line and then dry run it with some data. Never leave this step out. Then plan the change: which line must be different, and what will the new line be? There are two kinds of edit. You can change what it does, for example a new number, condition or message. Or you can add a task, which means new lines, and sometimes a new variable. After the edit, test again with a range of data, including boundary values. One test is not enough. If the edited algorithm gives a wrong result, go back through the steps and try again.
Read it→Dry run it→Plan the change→Edit itif wrong, repeat→Test again
Worked examples
Pseudocode
1INPUT people
2IF people <= 6 THEN
3 OUTPUT "Lift is OK"4ELSE
5 OUTPUT "Too many people"6ENDIF
A lift sign. The lift takes 6 people. With 5 and 6 people typed the output is Lift is OK, because 6 <= 6 is true. With 7 people it says Too many people. Reading it first shows that the number 6 sits in the condition.
Test data
Expected output
Output of edited algorithm
3
Lift is OK
Lift is OK
8
Lift is OK
Lift is OK
9
Too many people
Too many people
20
Too many people
Too many people
Edit 1: a bigger lift takes 8 people. Only one number has to change, in line 2. The testing matters most. The data 8 and 9 sit on the boundary, so they show whether <= is right. Data such as 3 would give Lift is OK before and after the edit, so it proves little.
Pseudocode
1INPUT people
2IF people <= 8 THEN
3 OUTPUT "Lift is OK"4ELSE
5 extra ← people - 86 OUTPUT "Too many. Please leave: ", extra
7ENDIF
Edit 2: add a task. The sign must also say how many people have to get out. A new variable extra is made in the ELSE branch, because it is only needed there. With 11 people typed the output is Too many. Please leave: 3. With 9 people it is Too many. Please leave: 1.
Class activity
Edit swap (no computer). Each player writes a short algorithm on paper, for example a canteen sign that shows Open if the hour typed is between 8 and 14, and Closed otherwise, using AND. Swap papers. Your partner dry runs it with three test numbers, then makes one change (a new time) and one extra task (for example, also show Lunch served when the hour is 12). Then you check each other’s edits with boundary values. Talk about which test values were most useful. If you have a computer or tablet, an adult can help you type one edited algorithm in Python.
Practice
1
Dry run this algorithm with 1, 2, 5 and 6 hours. What is the fee each time?
Edit the parking algorithm in task 1 so that the first 3 hours are free and each extra hour costs 4. Write the new lines. Then complete the table of expected fees for the edited algorithm.
Hours typed
Expected fee after the edit
3
?
4
?
7
?
IF hours > 3 THEN / fee ← (hours - 3) * 4 / ENDIF (the rest stays the same). Expected fees: 3 hours gives 0, 4 hours gives 4, 7 hours gives 16.
3
The parking algorithm must also show Long stay, as an extra output, when the hours are more than 8. The fee must still be shown. Write the extra lines and say where they go.
After the first ENDIF (or after OUTPUT) add: IF hours > 8 THEN / OUTPUT "Long stay" / ENDIF. It is a new, separate IF, so the fee is still shown. Putting the new lines inside the first IF would be wrong, because they would only run when hours > 2 (or 3).
4
A pupil changes the lift limit from 6 to 8, tests with 3 people and says it works. Why is this not enough? Name three better test values and say why they are better.
A value like 3 gives Lift is OK both before and after the change, so it cannot show a mistake. Better: 8 (a boundary: it must be OK), 9 (just over the boundary: it must be too many) and a large value such as 20 (clearly too many). The boundary values find mistakes such as < instead of <=.