Common array mistakes in pseudocode

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

Common array mistakes in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. DECLARE Scores : ARRAY[1:30] OF INTEGER. Which loop header visits every element safely?
A FOR i ← 1 TO 30
B FOR i ← 1 TO 31
2. DECLARE Names : ARRAY[0:9] OF STRING. Which loop header reaches every element?
A FOR i ← 0 TO 9
B FOR i ← 1 TO 9
3. The array is ARRAY[0:9]. What is the problem with FOR i ← 1 TO 10?
A There is no problem because it still runs 10 times
B It skips index 0 and goes out of range at index 10
4. Grid is ARRAY[1:3, 1:5] (3 rows, 5 columns). Which reads row 2, column 4?
A Grid[4, 2]
B Grid[2, 4]
5. DECLARE Marks : ARRAY[1:5] OF INTEGER. Which statement is valid?
A Marks[1] ← 87
B Marks[1] ← "eighty-seven"

Match the Pairs

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

Left columnRight column
1FOR Index ← 1 TO 31 (array is ARRAY[1:30]) AFOR Index ← 0 TO 9
2Grid[Column, Row] (array is ARRAY[1:3, 1:5]) BTotal ← 0 FOR Index ← 1 TO 30 Total ← Total + Scores[Index] NEXT Index
3A loop that fills ARRAY[0:9] must start at the lower bound of 0, not 1 CLoop limit goes past the upper bound, so the last pass gives an index out of range error
4A running total must be set back to 0 before the loop that adds up the elements DRow and column indexes are swapped, so the wrong element or an out of range index is used
5DECLARE Names : ARRAY[1:5] OF STRING Names[1] ← 42 EThe loop starts at 1 and ends at 10, but the array bounds are 0 to 9. It skips element 0 and index 10 is out of range. Use FOR Index ← 0 TO 9.
6DECLARE Marks : ARRAY[0:9] OF INTEGER FOR Index ← 1 TO 10 Marks[Index] ← 0 NEXT Index FA number is stored in a STRING array. Each element must hold the declared data type, so store text such as "Ava" instead.

Definitions

common array mistakes

Fill in the Blanks

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

1.
DECLARE Score : ARRAY[1:30] OF INTEGER FOR i ← 1 TO __ NEXT i
2.
DECLARE Name : ARRAY[0:9] OF STRING FOR Index ← _ TO 9 NEXT Index
3.
DECLARE Grid : ARRAY[1:3,1:5] OF INTEGER FOR Row ← 1 TO 3 Grid[___, 2] ← 0
4.
_____ ← 0 FOR i ← 1 TO 10 Total ← Total + Mark[i]
5.
DECLARE Price : ARRAY[1:5] OF ____ Price[1] ← 4.5

Spot the Error

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

Question 1
A DECLARE D:ARRAY[0:6] OF REAL
B Total ← 0
C FOR i ← 1 TO 6
D Total ← Total + D[i]
E NEXT i
F OUTPUT Total
Question 2
A DECLARE T:ARRAY[1:4] OF REAL
B FOR i ← 0 TO 4
C T[i] ← 0
D NEXT i
Question 3
A DECLARE N:ARRAY[1:5] OF CHAR
B N[1] ← 12.5
C N[2] ← 7.25
D OUTPUT N[1] + N[2]
Question 4
A DECLARE S:ARRAY[1:30] OF REAL
B Total ← 0
C FOR i ← 1 TO 31
D Total ← Total + S[i]
E NEXT i
Question 5
A // G is ARRAY[1:3, 1:4]
B FOR Row ← 1 TO 3
C FOR Col ← 1 TO 5
D G[Row, Col] ← 0
E NEXT Col
F NEXT Row

Answer Key

Common array mistakes in pseudocode
Section A: Quick Questions
Q1
A — FOR i ← 1 TO 30
Q2
A — FOR i ← 0 TO 9
Q3
B — It skips index 0 and goes out of range at index 10
Q4
B — Grid[2, 4]
Q5
A — Marks[1] ← 87
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 31 (array is ARRAY[1:30]) → Loop limit goes past the upper bound, so the last pass gives an index out of range error
Q2
Grid[Column, Row] (array is ARRAY[1:3, 1:5]) → Row and column indexes are swapped, so the wrong element or an out of range index is used
Q3
A loop that fills ARRAY[0:9] must start at the lower bound of 0, not 1 → FOR Index ← 0 TO 9
Q4
A running total must be set back to 0 before the loop that adds up the elements → Total ← 0 FOR Index ← 1 TO 30 Total ← Total + Scores[Index] NEXT Index
Q5
DECLARE Names : ARRAY[1:5] OF STRING Names[1] ← 42 → A number is stored in a STRING array. Each element must hold the declared data type, so store text such as "Ava" instead.
Q6
DECLARE Marks : ARRAY[0:9] OF INTEGER FOR Index ← 1 TO 10 Marks[Index] ← 0 NEXT Index → The loop starts at 1 and ends at 10, but the array bounds are 0 to 9. It skips element 0 and index 10 is out of range. Use FOR Index ← 0 TO 9.
Section C: Definitions
Q1
common array mistakes — Typical bugs: loop limits not matching ARRAY[l:u], swapped row and column indexes, a total not reset to 0, or the wrong data type stored. Check bounds, index order and starting values.
Section D: Fill in the Blanks
Q1
DECLARE Score : ARRAY[1:30] OF INTEGER FOR i ← 1 TO 30 NEXT i
Q2
DECLARE Name : ARRAY[0:9] OF STRING FOR Index ← 0 TO 9 NEXT Index
Q3
DECLARE Grid : ARRAY[1:3,1:5] OF INTEGER FOR Row ← 1 TO 3 Grid[Row, 2] ← 0
Q4
Total ← 0 FOR i ← 1 TO 10 Total ← Total + Mark[i]
Q5
DECLARE Price : ARRAY[1:5] OF REAL Price[1] ← 4.5
Section E: Spot the Error
Q1
C — The array starts at index 0, so a loop starting at 1 skips D[0] and the total is too small. It should be FOR i ← 0 TO 6.
Q2
A — The loop uses index 0, but the declaration starts at 1, so T[0] is out of range. The declaration should be ARRAY[0:4].
Q3
A — The array stores decimal numbers, but it is declared as CHAR, so the wrong data type is used and the sum makes no sense. It should be OF REAL.
Q4
C — The array only has indexes 1 to 30, so the loop reaches S[31], which is out of range. It should be FOR i ← 1 TO 30.
Q5
C — G has only 4 columns, so the inner loop must stop at 4. Going to 5 uses a column that does not exist. It should be FOR Col ← 1 TO 4.