Writing an algorithm for a described problem

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

Writing an algorithm for a described problem

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A teacher wants the total of 30 marks. Which start value is correct for the total before the loop?
A Total ← 30
B Total ← 0
2. A problem says: 'Find the highest mark in a class of 25 students.' Which standard method is needed?
A Bubble sort
B Finding the maximum
3. Which is the better identifier name for a variable that stores the number of students who passed?
A PassCount
B x
4. Which order is best when writing an algorithm for a described problem?
A List the inputs, choose the standard method, then write the pseudocode
B Write the loop first, then work out what the problem is asking
5. A class has 30 marks stored in an array. To calculate the average mark, what should the algorithm do after the loop has finished?
A Divide Total by the number of marks, then OUTPUT the result
B Divide Total by the number of marks inside the loop on every repeat

Match the Pairs

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

Left columnRight column
1DECLARE Total : INTEGER Total ← 0 AThe comparison uses < instead of >, so this finds the lowest mark rather than the highest. It should be Marks[Index] > Highest.
2FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index BAverage ← Total / 30 OUTPUT "Average: ", Average
3After the loop has finished, work out the class average from the total and the number of marks, then show it. CIF Marks[Index] >= 50 THEN Passes ← Passes + 1 ENDIF
4To count the passes, start a counter at 0 and add 1 only when a mark meets the pass condition. DLoop through all 30 marks in the array and add each one to the running total.
5FOR Index ← 1 TO 30 Total ← 0 Total ← Total + Marks[Index] NEXT Index ETotal is reset to 0 inside the loop, so it is wiped on every repeat and only the last mark is kept. The start value must be set before the loop.
6Highest ← 0 FOR Index ← 1 TO 30 IF Marks[Index] < Highest THEN Highest ← Marks[Index] ENDIF NEXT Index FDeclare the variable and set its start value to 0 before the loop, so the running total begins correctly.

Definitions

DECLARE, loop, OUTPUT

Fill in the Blanks

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

1.
_______ Total : INTEGER
2.
DECLARE Total : INTEGER Total ← _
3.
FOR Index ← 1 TO 30 Total ← Total + Marks[_____] NEXT Index
4.
______ "Highest mark: ", Highest
5.
IF Marks[Index] __ 50 THEN Passes ← Passes + 1 ENDIF

Spot the Error

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

Question 1
A DECLARE Total : INTEGER
B Total ← 0
C FOR i ← 1 TO 5
D Total ← Mark[i]
E NEXT i
F OUTPUT Total
Question 2
A Total ← 0
B FOR i ← 1 TO 4
C Total ← Total + Mark[i]
D NEXT i
E Average ← Total / 5
F OUTPUT Average
Question 3
A Found ← FALSE
B FOR i ← 1 TO 5
C IF Name[i] ← Target THEN
D Found ← TRUE
E ENDIF
F NEXT i
Question 4
A IF Score[1] > Score[2] THEN
B Temp ← Score[2]
C Score[1] ← Score[2]
D Score[2] ← Temp
E ENDIF
Question 5
A DECLARE Found : INTEGER
B Found ← FALSE
C OUTPUT Found

Answer Key

Writing an algorithm for a described problem
Section A: Quick Questions
Q1
B — Total ← 0
Q2
B — Finding the maximum
Q3
A — PassCount
Q4
A — List the inputs, choose the standard method, then write the pseudocode
Q5
A — Divide Total by the number of marks, then OUTPUT the result
Section B: Match the Pairs
Q1
DECLARE Total : INTEGER Total ← 0 → Declare the variable and set its start value to 0 before the loop, so the running total begins correctly.
Q2
FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index → Loop through all 30 marks in the array and add each one to the running total.
Q3
After the loop has finished, work out the class average from the total and the number of marks, then show it. → Average ← Total / 30 OUTPUT "Average: ", Average
Q4
To count the passes, start a counter at 0 and add 1 only when a mark meets the pass condition. → IF Marks[Index] >= 50 THEN Passes ← Passes + 1 ENDIF
Q5
FOR Index ← 1 TO 30 Total ← 0 Total ← Total + Marks[Index] NEXT Index → Total is reset to 0 inside the loop, so it is wiped on every repeat and only the last mark is kept. The start value must be set before the loop.
Q6
Highest ← 0 FOR Index ← 1 TO 30 IF Marks[Index] < Highest THEN Highest ← Marks[Index] ENDIF NEXT Index → The comparison uses < instead of >, so this finds the lowest mark rather than the highest. It should be Marks[Index] > Highest.
Section C: Definitions
Q1
DECLARE, loop, OUTPUT — To solve a described problem, list the inputs, pick the standard method, then write it: DECLARE variables, set start values, loop through the data, and OUTPUT the result.
Section D: Fill in the Blanks
Q1
DECLARE Total : INTEGER
Q2
DECLARE Total : INTEGER Total ← 0
Q3
FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index
Q4
OUTPUT "Highest mark: ", Highest
Q5
IF Marks[Index] >= 50 THEN Passes ← Passes + 1 ENDIF
Section E: Spot the Error
Q1
D — This line replaces Total with each mark instead of adding to it, so the loop ends with only the last mark. It should be Total ← Total + Mark[i].
Q2
B — The class has 5 marks, but the loop stops at 4, so the fifth mark is never added to the total. It should be FOR i ← 1 TO 5.
Q3
C — The arrow ← is for assignment. A search condition must compare values with =, so it should be IF Name[i] = Target THEN.
Q4
B — Temp must store the first score before it is overwritten. With Score[2] stored, the original Score[1] is lost and the swap fails. It should be Temp ← Score[1].
Q5
A — A Found flag holds TRUE or FALSE, so its data type must be BOOLEAN, not INTEGER. It should be DECLARE Found : BOOLEAN.