Completing a partly written algorithm

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

Completing a partly written algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A totalling loop adds 10 marks. What goes in the missing line? ___ FOR Index ← 1 TO 10 Total ← Total + Mark[Index] NEXT Index
A Total ← 1
B Total ← 0
2. Finding the maximum in Mark[1:5]. Which start value is best for Max before the loop?
A Max ← Mark[1]
B Max ← 100
3. Name[1:30] holds 30 names and every one must be checked. Which loop line is missing the right bound?
A FOR Index ← 1 TO 29
B FOR Index ← 1 TO 30
4. Count the marks that are MORE than 50. Which condition completes the line? IF Mark[Index] ___ 50 THEN
A >
B >=
5. A FOR loop contains an IF ... THEN statement. The IF is complete, but the line IF Score > Max THEN has no closing line. Which keyword is missing?
A ENDIF
B NEXT

Match the Pairs

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

Left columnRight column
1Total ← ___ FOR i ← 0 TO 4 Total ← Total + Marks[i] NEXT i AThe blank must be the last index of the array, such as 4 for an array of 5 items
2FOR i ← 0 TO ___ OUTPUT Marks[i] NEXT i BMax starts at 0, which fails if all values are negative. Max should start as the first array element, Marks[0].
3A counting algorithm needs a closing keyword for its IF statement inside the loop, so the missing line is ENDIF CFOR i ← 0 TO 9 IF Score[i] > 50 THEN Count ← Count + 1 ENDIF NEXT i
4A linear search with a WHILE loop must keep going only while the item is not found and the end of the list is not reached DThe blank must be 0, so the total starts empty before any marks are added
5Max ← 0 FOR i ← 0 TO 4 IF Marks[i] > Max THEN Max ← Marks[i] ENDIF NEXT i EWHILE Found = FALSE AND i <= 9 DO IF Names[i] = Target THEN Found ← TRUE ENDIF i ← i + 1 ENDWHILE
6REPEAT INPUT Num Total ← Total + Num ENDWHILE FThe wrong closing keyword is used. A REPEAT loop must end with UNTIL and a condition, not ENDWHILE.

Definitions

complete the algorithm

Fill in the Blanks

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

1.
Total ← _ FOR Index ← 1 TO 5 Total ← Total + Num[Index]
2.
DECLARE Mark : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO __ Total ← Total + Mark[Index]
3.
IF Mark[Index] _ Max THEN Max ← Mark[Index] ENDIF
4.
FOR Index ← 1 TO 10 Total ← Total + Mark[Index] ____ Index
5.
REPEAT INPUT Guess _____ Guess = 42

Spot the Error

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

Question 1
A Max ← Score[1]
B FOR i ← 2 TO 8
C IF Score[i] < Max THEN
D Max ← Score[i]
E ENDIF
F NEXT i
Question 2
A Count ← 1
B FOR i ← 1 TO 10
C IF Mark[i] > 50 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Question 3
A Sum ← 0
B FOR i ← 1 TO 4
C INPUT Mark
D Sum ← Sum + Mark
E NEXT i
F OUTPUT Sum / 5
Question 4
A Total ← 0
B FOR i ← 1 TO 10
C Total ← Total + Mark[i]
D ENDFOR
E OUTPUT Total
Question 5
A Min ← 0
B FOR i ← 2 TO 7
C IF Temp[i] < Min THEN
D Min ← Temp[i]
E ENDIF
F NEXT i

Answer Key

Completing a partly written algorithm
Section A: Quick Questions
Q1
B — Total ← 0
Q2
A — Max ← Mark[1]
Q3
B — FOR Index ← 1 TO 30
Q4
A — >
Q5
A — ENDIF
Section B: Match the Pairs
Q1
Total ← ___ FOR i ← 0 TO 4 Total ← Total + Marks[i] NEXT i → The blank must be 0, so the total starts empty before any marks are added
Q2
FOR i ← 0 TO ___ OUTPUT Marks[i] NEXT i → The blank must be the last index of the array, such as 4 for an array of 5 items
Q3
A counting algorithm needs a closing keyword for its IF statement inside the loop, so the missing line is ENDIF → FOR i ← 0 TO 9 IF Score[i] > 50 THEN Count ← Count + 1 ENDIF NEXT i
Q4
A linear search with a WHILE loop must keep going only while the item is not found and the end of the list is not reached → WHILE Found = FALSE AND i <= 9 DO IF Names[i] = Target THEN Found ← TRUE ENDIF i ← i + 1 ENDWHILE
Q5
Max ← 0 FOR i ← 0 TO 4 IF Marks[i] > Max THEN Max ← Marks[i] ENDIF NEXT i → Max starts at 0, which fails if all values are negative. Max should start as the first array element, Marks[0].
Q6
REPEAT INPUT Num Total ← Total + Num ENDWHILE → The wrong closing keyword is used. A REPEAT loop must end with UNTIL and a condition, not ENDWHILE.
Section C: Definitions
Q1
complete the algorithm — Fill each missing line or value by asking what the standard method (total, count, max, search, sort) needs at that point: start value, loop bound, condition or closing keyword.
Section D: Fill in the Blanks
Q1
Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Num[Index]
Q2
DECLARE Mark : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO 10 Total ← Total + Mark[Index]
Q3
IF Mark[Index] > Max THEN Max ← Mark[Index] ENDIF
Q4
FOR Index ← 1 TO 10 Total ← Total + Mark[Index] NEXT Index
Q5
REPEAT INPUT Guess UNTIL Guess = 42
Section E: Spot the Error
Q1
C — To find the maximum, the condition must check for a score greater than Max. Using < would find the minimum instead. It should be IF Score[i] > Max THEN.
Q2
A — A counter must start at 0 so that it counts only the items that meet the condition. Starting at 1 makes the final count one too high. It should be Count ← 0.
Q3
B — There are 5 marks, so the loop bound must be 5. With TO 4 the last mark is never added, but the total is still divided by 5. It should be FOR i ← 1 TO 5.
Q4
D — In Cambridge pseudocode a FOR loop is closed with NEXT, not ENDFOR. The line should be NEXT i.
Q5
A — The minimum must start as the first item in the array. Starting at 0 means no temperature above 0 can ever be smaller, so the result would be wrong. It should be Min ← Temp[1].