Week 24 · Unit 10: Be a game developer · 3 lessons
1
Adding and subtracting
Computing 5: Unit 10, pp. 129–144
GoalUse + and − in an algorithm to calculate with variables and store the result.
New words
arithmetic operator · a symbol such as + or − that tells the computer to calculatecalculate · to work out an answer with numberstotal · the answer when numbers are addeddifference · the answer when one number is taken away from another
Explanation
Arithmetic operators let a program calculate. The plus sign adds numbers and the minus sign takes one number away from another. The numbers can be constants or variables. The answer can be stored in a new variable. Suppose an algorithm asks for two numbers and stores them in X and Y. It can then set Sum to X + Y and output Sum. Each time the algorithm runs, the user can enter new numbers, so X, Y and Sum hold new values. The steps stay the same.
Run
X
Y
Sum = X + Y
Gap = X − Y
1
9
4
13
5
2
6
6
12
0
3
20
7
27
13
Worked examples
Step
Instruction
Values
1
Ask for the number of children and store it in Children
Children = 14
2
Ask for the number of adults and store it in Adults
Adults = 8
3
Set Visitors to Children + Adults
Visitors = 22
4
Output Visitors
shows 22
This algorithm counts the visitors to a museum. It asks for the number of children and the number of adults, adds them, and outputs the total. With 14 children and 8 adults the total is 22.
Step
Instruction
Values
1
Ask for Red’s points
Red = 17
2
Ask for Blue’s points
Blue = 11
3
Set Gap to Red − Blue
Gap = 6
4
Output Gap
shows 6
Subtraction finds the difference between two numbers. This algorithm tells how many more points the red team has than the blue team. Red is 17 and Blue is 11, so Gap is 6.
Here is the museum algorithm as a Scratch program. Each ask block puts the typed text in the reporter answer. The set blocks copy it into a variable. The add block sits inside the set block. If the user types 14 and then 8, the sprite says ‘Visitors: 22’.
Class activity
Number cups (3 players, 3 paper cups labelled X, Y and Sum, and about 30 beans or counters). Player 1 rolls a die twice and puts that many beans in cup X and in cup Y. Player 2 empties both cups into the cup Sum and counts: this is X + Y. Now return the beans and do a subtraction: Player 3 takes Y beans away from X and says the difference. What happens if Y is bigger than X? Play 5 rounds and write the values of X, Y and the answer in a table.
Practice
1
X holds 8 and Y holds 5. What are X + Y and X − Y?
X + Y = 13 and X − Y = 3.
2
The user types 15 and then 6. What does the sprite say?
The sprite says 9, because 15 − 6 = 9.
3
Write an algorithm in numbered steps. It asks for the number of boys and the number of girls in a class and outputs how many more girls than boys there are. Assume there are more girls.
1 Ask for the number of boys and store it in Boys. 2 Ask for the number of girls and store it in Girls. 3 Set More to Girls − Boys. 4 Output More.
4
A variable called points holds 12. Work through the steps and say what points holds at the end.