Working out the purpose of an algorithm

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Working out the purpose of an algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An algorithm sets total to 0, adds each price to total, then SHOWS total. What is its purpose?
A To count how many prices there are
B To work out the total cost of all the prices
2. What does the purpose of an algorithm tell us?
A What the whole algorithm achieves
B What one single step does
3. An algorithm sets count to 0, goes through each student, adds 1 to count if the student is present, then SHOWS count. Which sentence is its purpose?
A It shows the name of every student.
B It counts how many students are present.
4. An algorithm uses variables named highScore and playerScore. If playerScore is bigger than highScore, it copies playerScore into highScore. What do the names hint at?
A It keeps track of the highest score
B It adds up all the scores
5. An algorithm ends with: IF temperature < 15 THEN SHOW 'Take a jumper!'. What is the best purpose?
A To tell someone if they need a jumper
B To measure the temperature

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SET total TO 0 REPEAT 5 TIMES ADD score TO total SHOW total AThis only describes one part of the algorithm. A purpose says what the whole algorithm achieves, not just one step or variable.
2IF temperature > 30 THEN SHOW "Wear a hat" ELSE SHOW "No hat needed" BIF age >= 18 THEN SHOW "You can vote" ELSE SHOW "Too young to vote"
3Variable names are clues to the purpose CSET lapsRun TO 0 REPEAT UNTIL lapsRun = 10 ADD 1 TO lapsRun SHOW lapsRun
4The SHOW steps tell you what the algorithm achieves DThis just repeats the steps in order. It does not say what the algorithm achieves.
5Purpose: it uses a variable called total EPurpose: tells you whether to wear a hat
6Purpose: SET count TO 0, then REPEAT 3 TIMES, then SHOW count FPurpose: adds up five scores and shows the total

Definitions

purpose

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
SET total ← 0 SET total ← total + 8 ____ total
2.
SET lives ← 3 IF _____ = 0 THEN SHOW "Game over"
3.
SET cost ← 35 SET change ← 50 _ cost SHOW change
4.
SET score ← 62 IF score >= 50 THEN SHOW "Pass" ____ SHOW "Try again"
5.
SET cheer ← "Hooray!" ______ 3 TIMES SHOW cheer

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A SET total TO 0
B ASK cost
C SET total TO total + price
D SHOW total
Question 2
A SET steps TO 0
B REPEAT 10 TIMES
C SET steps TO steps + 1
D SHOW step
Question 3
A SET evens TO 0
B ASK number
C IF number MOD 2 = 1 THEN
D SET evens TO evens + 1
E SHOW evens
Question 4
A SET highest TO 0
B ASK score
C IF score < highest THEN
D SET highest TO score
E SHOW highest
Question 5
A SET savings TO 0
B REPEAT 5 TIMES
C SET savings TO savings + 5
D SHOW "Money after 4 weeks"

Answer Key

Working out the purpose of an algorithm
Section A: Quick Questions
Q1
B — To work out the total cost of all the prices
Q2
A — What the whole algorithm achieves
Q3
B — It counts how many students are present.
Q4
A — It keeps track of the highest score
Q5
A — To tell someone if they need a jumper
Section B: Match the Pairs
Q1
SET total TO 0 REPEAT 5 TIMES ADD score TO total SHOW total → Purpose: adds up five scores and shows the total
Q2
IF temperature > 30 THEN SHOW "Wear a hat" ELSE SHOW "No hat needed" → Purpose: tells you whether to wear a hat
Q3
Variable names are clues to the purpose → SET lapsRun TO 0 REPEAT UNTIL lapsRun = 10 ADD 1 TO lapsRun SHOW lapsRun
Q4
The SHOW steps tell you what the algorithm achieves → IF age >= 18 THEN SHOW "You can vote" ELSE SHOW "Too young to vote"
Q5
Purpose: it uses a variable called total → This only describes one part of the algorithm. A purpose says what the whole algorithm achieves, not just one step or variable.
Q6
Purpose: SET count TO 0, then REPEAT 3 TIMES, then SHOW count → This just repeats the steps in order. It does not say what the algorithm achieves.
Section C: Definitions
Q1
purpose — What the whole algorithm achieves. Follow the steps, check what it SHOWS and read the variable names for clues, then say it in one sentence.
Section D: Fill in the Blanks
Q1
SET total ← 0 SET total ← total + 8 SHOW total
Q2
SET lives ← 3 IF lives = 0 THEN SHOW "Game over"
Q3
SET cost ← 35 SET change ← 50 - cost SHOW change
Q4
SET score ← 62 IF score >= 50 THEN SHOW "Pass" ELSE SHOW "Try again"
Q5
SET cheer ← "Hooray!" REPEAT 3 TIMES SHOW cheer
Section E: Spot the Error
Q1
B — The algorithm adds up prices, so line 2 should ASK price. It asks for cost, but line 3 uses price, so the total would never be worked out properly.
Q2
D — The variable is called steps, not step. The purpose is to count 10 steps and show the answer, so line 4 should be SHOW steps.
Q3
C — The variable name evens tells us the purpose is to count even numbers. An even number has a remainder of 0 when divided by 2, so the test should be MOD 2 = 0.
Q4
C — The purpose is to find the highest score. The new score should replace highest only when it is bigger, so the test should be score > highest.
Q5
B — The SHOW step says the money is after 4 weeks, so the loop should repeat 4 times, one for each week. Repeating 5 times does not match the purpose.