AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Numbers on the board

Week 18 · Unit 8: Be an innovator · 3 lessons
2

Algorithms that calculate

Computing 6: Unit 8, pp. 106–123
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.

Startenter the lengthenter the widthset perimeter to (length + width) × 2show the perimeterEnd
Worked examples
Steplengthwidthperimeter
after the inputs940
after the calculation9426
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.
Startenter score 1, score 2 and score 3set total to score 1 + score 2 + score 3set average to total ÷ 3show the averageEnd
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?
Startenter the lengthenter the width?show the areaEnd
2
A program asks for three numbers and shows their average. The numbers are 4, 10 and 7. What does it show?
3
Write an algorithm that changes minutes into seconds. What does it show for 5 minutes?
4
The program should show half of the number, but it shows 36 when the number is 18. Find the bug and fix it.
setnto18setoutton×2show numberouton buttonApressed