GoalWrite and follow an algorithm that takes numbers in, calculates with operators and shows the answer.
New words
input · data that goes into a program, such as a number typed or a button pressedprocess · what the program does with the input, such as a calculationoutput · what the program shows or sends outbracket · a pair of marks that tells us to work out what is inside first
Explanation
Many algorithms follow the same three parts. The input is the data that goes in. The process is the calculation. The output is the answer that comes out. A flowchart shows the input and output in parallelograms and the calculation in a rectangle. To write the algorithm, first say what we want to know. Then list the inputs and decide which operators to use. Then give each value a clear name. Brackets show which part to work out first, so (length + width) × 2 is not the same as length + width × 2. At the end, try the algorithm with test numbers and check the answer by hand.
Worked examples
Step
length
width
perimeter
after the inputs
9
4
0
after the calculation
9
4
26
Trace the perimeter algorithm with a length of 9 and a width of 4. We work out the part in brackets first: 9 + 4 = 13. Then we multiply: 13 × 2 = 26. The output is 26.
This algorithm finds the average of three scores. It adds them, and then divides by 3. With the scores 7, 9 and 8, the total is 24 and 24 ÷ 3 = 8.
A common mistake is to leave out the brackets. Written as length + width × 2, the multiplication is worked out first, so 9 + 4 × 2 = 9 + 8 = 17. That is not the perimeter. The brackets give the correct answer, 26.
Class activity
Number machine (no computer): one player is the machine with a secret rule, such as ‘double the number, then add 3’. Another player gives a number, and the machine says the output. Do the machine with 2, 5 and 10 and write the input and output pairs in a table. The other players guess the rule from the table. Then swap roles and invent a rule with two operators. Write the rule as an algorithm with input, process and output. If you have a computer or tablet, build the machine in a micro:bit simulator.
Practice
1
Complete the algorithm for the area of a rectangle. What is the output when the length is 8 and the width is 5?
The missing process is: set area to length × width. The output is 8 × 5 = 40.
2
A program asks for three numbers and shows their average. The numbers are 4, 10 and 7. What does it show?
4 + 10 + 7 = 21, and 21 ÷ 3 = 7. It shows 7.
3
Write an algorithm that changes minutes into seconds. What does it show for 5 minutes?
1 Start. 2 Enter the minutes. 3 Set seconds to minutes × 60. 4 Show the seconds. 5 Stop. For 5 minutes it shows 5 × 60 = 300.
4
The program should show half of the number, but it shows 36 when the number is 18. Find the bug and fix it.
The program multiplies by 2 instead of dividing by 2. Change the block to (n) ÷ (2). Then it shows 18 ÷ 2 = 9.