The purpose of an algorithm

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

The purpose of an algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is a statement of the PURPOSE of an algorithm?
A Finds the largest of ten numbers
B Sets Max to 0, then loops 10 times and compares each number
2. An exam asks: 'State the purpose of the algorithm.' Which answer is better?
A Line 1 declares Total, line 2 sets it to 0, line 3 starts a FOR loop
B Calculates the average of five test marks
3. Where is the best place to look first for clues about what an algorithm does?
A The OUTPUT statement at the end
B The comment on the first line only
4. The algorithm ends with OUTPUT "Total cost is ", TotalCost. What is the most likely purpose?
A Calculates and displays a total cost
B Counts how many times a loop runs
5. An algorithm uses the identifiers Highest, Score and Count. What do these names suggest?
A Identifier names never give any clues
B It probably finds the highest score

Match the Pairs

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

Left columnRight column
1Max ← Number[1] FOR Index ← 2 TO 10 IF Number[Index] > Max THEN Max ← Number[Index] NEXT Index OUTPUT Max AThis only names the programming constructs used. It does not say what the algorithm achieves, and it ignores the OUTPUT and identifier names.
2Total ← 0 FOR Count ← 1 TO 5 INPUT Mark Total ← Total + Mark NEXT Count OUTPUT Total / 5 BTotalCost ← TotalCost + Price
3Start from the OUTPUT: the value that is output shows what the algorithm is trying to achieve. CThis is a line-by-line description of the steps, not the purpose. It does not say what the whole algorithm achieves.
4Clues in identifier names: meaningful names hint at the purpose of an algorithm. DPurpose: calculates the average of five marks entered by the user.
5Purpose: Sets Total to 0, then adds each Mark to Total in a loop. EOUTPUT PassCount
6Purpose: Uses a FOR loop and an IF statement. FPurpose: finds the largest of ten numbers stored in an array.

Definitions

purpose

Fill in the Blanks

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

1.
// Purpose: finds the largest number IF Number _ Largest THEN Largest ← Number
2.
Total ← Total + Mark Average ← Total / 5 ______ "Average mark: ", Average
3.
IF Age >= 18 THEN AdultCount ← __________ + 1 ENDIF
4.
Process: Remainder ← Number ___ 2 Decision: Remainder = 0? Output: "Even"
5.
FOR Count ← 1 __ 10 INPUT Number Total ← Total + Number

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 1 TO 10
C INPUT Num
D Total ← Num
E NEXT Count
F OUTPUT Total
Question 2
A Passes ← 10
B INPUT Mark
C IF Mark >= 50 THEN
D Passes ← Passes + 1
E ENDIF
F OUTPUT Passes
Question 3
A INPUT Mark1
B INPUT Mark2
C Average ← Mark1 + Mark2 / 2
D OUTPUT Average
Question 4
A INPUT Length
B INPUT Height
C Area ← Length * Width
D OUTPUT Area
Question 5
A INPUT Price
B Discount ← Price + 0.1
C NewPrice ← Price - Discount
D OUTPUT NewPrice

Answer Key

The purpose of an algorithm
Section A: Quick Questions
Q1
A — Finds the largest of ten numbers
Q2
B — Calculates the average of five test marks
Q3
A — The OUTPUT statement at the end
Q4
A — Calculates and displays a total cost
Q5
B — It probably finds the highest score
Section B: Match the Pairs
Q1
Max ← Number[1] FOR Index ← 2 TO 10 IF Number[Index] > Max THEN Max ← Number[Index] NEXT Index OUTPUT Max → Purpose: finds the largest of ten numbers stored in an array.
Q2
Total ← 0 FOR Count ← 1 TO 5 INPUT Mark Total ← Total + Mark NEXT Count OUTPUT Total / 5 → Purpose: calculates the average of five marks entered by the user.
Q3
Start from the OUTPUT: the value that is output shows what the algorithm is trying to achieve. → OUTPUT PassCount
Q4
Clues in identifier names: meaningful names hint at the purpose of an algorithm. → TotalCost ← TotalCost + Price
Q5
Purpose: Sets Total to 0, then adds each Mark to Total in a loop. → This is a line-by-line description of the steps, not the purpose. It does not say what the whole algorithm achieves.
Q6
Purpose: Uses a FOR loop and an IF statement. → This only names the programming constructs used. It does not say what the algorithm achieves, and it ignores the OUTPUT and identifier names.
Section C: Definitions
Q1
purpose — What the whole algorithm achieves, stated in one sentence, such as 'finds the largest of ten numbers'. It is not a line-by-line description. Check the OUTPUT and identifier names for clues.
Section D: Fill in the Blanks
Q1
// Purpose: finds the largest number IF Number > Largest THEN Largest ← Number
Q2
Total ← Total + Mark Average ← Total / 5 OUTPUT "Average mark: ", Average
Q3
IF Age >= 18 THEN AdultCount ← AdultCount + 1 ENDIF
Q4
Process: Remainder ← Number MOD 2 Decision: Remainder = 0? Output: "Even"
Q5
FOR Count ← 1 TO 10 INPUT Number Total ← Total + Number
Section E: Spot the Error
Q1
D — The purpose is to find the total of ten numbers. The OUTPUT is Total, so each Num must be added to Total. It should be Total ← Total + Num. Without this, Total just holds the last number.
Q2
A — The purpose is to count passes, so the counter Passes must start at 0. Starting at 10 makes the OUTPUT wrong. It should be Passes ← 0.
Q3
C — The identifier name and the OUTPUT show the purpose is to find the average of two marks. Without brackets only Mark2 is divided by 2. It should be Average ← (Mark1 + Mark2) / 2.
Q4
B — The OUTPUT is Area and the calculation uses Length and Width, so the purpose is to find the area of a rectangle. Width is never given a value because Height is input instead. It should be INPUT Width.
Q5
B — Start from the OUTPUT: NewPrice is the price after a 10% discount. Discount must be 10% of Price, so it should be Discount ← Price * 0.1, not Price + 0.1.