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

Scores and sums

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.

RunXYSum = X + YGap = X − Y
194135
266120
32072713
Worked examples
StepInstructionValues
1Ask for the number of children and store it in ChildrenChildren = 14
2Ask for the number of adults and store it in AdultsAdults = 8
3Set Visitors to Children + AdultsVisitors = 22
4Output Visitorsshows 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.
StepInstructionValues
1Ask for Red’s pointsRed = 17
2Ask for Blue’s pointsBlue = 11
3Set Gap to Red − BlueGap = 6
4Output Gapshows 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.
whenclickedaskHow many children?and waitsetchildrentoansweraskHow many adults?and waitsetadultstoanswersetvisitorstochildren+adultssayjoinVisitors: visitors
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?
2
The user types 15 and then 6. What does the sprite say?
whenclickedaskFirst number?and waitsetfirsttoansweraskSecond number?and waitsetsecondtoanswersetdifferencetofirst-secondsaydifference
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.
4
A variable called points holds 12. Work through the steps and say what points holds at the end.
StepInstructionpoints
1Set points to 1212
2Set points to points + 5?
3Set points to points − 3?