Writing a complete array program: declare, fill and output

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

Writing a complete array program: declare, fill and output

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which order shows the correct pattern for a complete array program?
A FOR loop with OUTPUT, FOR loop with INPUT, DECLARE the array
B DECLARE the array, FOR loop with INPUT, FOR loop with OUTPUT
2. Which line correctly declares a 1D array to hold 5 student names?
A DECLARE StudentNames : ARRAY[1:5] OF INTEGER
B DECLARE StudentNames : ARRAY[1:5] OF STRING
3. Which loop correctly fills the Marks array with 5 values typed in by the user?
A FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index
B FOR Index ← 1 TO 5 OUTPUT Marks[Index] NEXT Index
4. A program has DECLARE Marks : ARRAY[1:30] OF INTEGER. Which loop header visits every element?
A FOR Index ← 1 TO 30
B FOR Index ← 1 TO 31
5. Which is the best way to write the OUTPUT loop body for the array StudentNames?
A OUTPUT StudentNames[Index]
B OUTPUT StudentNames

Match the Pairs

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

Left columnRight column
1DECLARE Marks : ARRAY[1:5] OF INTEGER AFOR Row ← 1 TO 3 FOR Col ← 1 TO 4 INPUT Grid[Row, Col] NEXT Col NEXT Row
2FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index BFOR Index ← 1 TO 5 OUTPUT Names[Index], Marks[Index] NEXT Index
3After the array is filled, a second FOR loop with the same bounds shows every element on the screen. CThe loop goes up to 10 but the array only has indexes 1 to 5, so it goes out of range. The loop should be FOR Index ← 1 TO 5.
4A 2D grid is filled using a loop inside a loop, with one index for the row and one for the column. DOUTPUT Marks does not show each element. A second FOR loop is needed with OUTPUT Marks[Index] to display every element.
5DECLARE Marks : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index EStep 1 of the program: create the array before using it, with its bounds and data type.
6FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index OUTPUT Marks FStep 2 of the program: a loop that fills every element of the array with a value typed in by the user.

Definitions

DECLARE, FOR INPUT, FOR OUTPUT

Fill in the Blanks

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

1.
DECLARE Marks : _____[1:5] OF INTEGER
2.
FOR Count ← 1 TO 5 _____ Marks[Count] NEXT Count
3.
FOR Count ← 1 TO 5 OUTPUT Names[Count] NEXT _____
4.
FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 INPUT Grid[Row, ___]
5.
DECLARE Names : ARRAY[1:3] OF ______ FOR Index ← 1 TO 3 OUTPUT Names[Index]

Spot the Error

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

Question 1
A DECLARE Pay:ARRAY[1:3] OF REAL
B FOR i ← 1 TO 3
C INPUT Pay(i)
D NEXT i
Question 2
A FOR i ← 1 TO 3
B INPUT Fee[i]
C ENDFOR
D FOR i ← 1 TO 3
E OUTPUT Fee[i]
F NEXT i
Question 3
A DECLARE Pay:ARRAY[1:3] REAL
B FOR i ← 1 TO 3
C INPUT Pay[i]
D NEXT i
Question 4
A DECLARE Grd:ARRAY[1:4] OF CHAR
B FOR i ← 1 TO 5
C INPUT Grd[i]
D NEXT i
Question 5
A FOR i ← 1 TO 3
B INPUT Mark[j]
C NEXT i
D FOR i ← 1 TO 3
E OUTPUT Mark[i]
F NEXT i

Answer Key

Writing a complete array program: declare, fill and output
Section A: Quick Questions
Q1
B — DECLARE the array, FOR loop with INPUT, FOR loop with OUTPUT
Q2
B — DECLARE StudentNames : ARRAY[1:5] OF STRING
Q3
A — FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index
Q4
A — FOR Index ← 1 TO 30
Q5
A — OUTPUT StudentNames[Index]
Section B: Match the Pairs
Q1
DECLARE Marks : ARRAY[1:5] OF INTEGER → Step 1 of the program: create the array before using it, with its bounds and data type.
Q2
FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index → Step 2 of the program: a loop that fills every element of the array with a value typed in by the user.
Q3
After the array is filled, a second FOR loop with the same bounds shows every element on the screen. → FOR Index ← 1 TO 5 OUTPUT Names[Index], Marks[Index] NEXT Index
Q4
A 2D grid is filled using a loop inside a loop, with one index for the row and one for the column. → FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 INPUT Grid[Row, Col] NEXT Col NEXT Row
Q5
DECLARE Marks : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index → The loop goes up to 10 but the array only has indexes 1 to 5, so it goes out of range. The loop should be FOR Index ← 1 TO 5.
Q6
FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index OUTPUT Marks → OUTPUT Marks does not show each element. A second FOR loop is needed with OUTPUT Marks[Index] to display every element.
Section C: Definitions
Q1
DECLARE, FOR INPUT, FOR OUTPUT — The pattern of a complete array program: declare the array, use a FOR loop to INPUT a value into each element, then use a second FOR loop to OUTPUT every element.
Section D: Fill in the Blanks
Q1
DECLARE Marks : ARRAY[1:5] OF INTEGER
Q2
FOR Count ← 1 TO 5 INPUT Marks[Count] NEXT Count
Q3
FOR Count ← 1 TO 5 OUTPUT Names[Count] NEXT Count
Q4
FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 INPUT Grid[Row, Col]
Q5
DECLARE Names : ARRAY[1:3] OF STRING FOR Index ← 1 TO 3 OUTPUT Names[Index]
Section E: Spot the Error
Q1
C — Array elements are accessed with square brackets, not round ones. It should be INPUT Pay[i].
Q2
C — A FOR loop in IGCSE pseudocode is closed with NEXT and the loop variable, not ENDFOR. Line 3 should be NEXT i.
Q3
A — The declaration is missing the keyword OF before the data type. It should be DECLARE Pay:ARRAY[1:3] OF REAL.
Q4
B — The array's upper bound is 4, so the loop must stop at 4. Going to 5 would cause an index out of range error. It should be FOR i ← 1 TO 4.
Q5
B — The loop variable is i, but the index uses j, which has no value. The index must be the loop variable, so it should be INPUT Mark[i].