Why arrays are used

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

Why arrays are used

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A teacher wants to store 30 student names in a program. Which approach is better?
A 30 separate variables: Name1, Name2, Name3 ... Name30
B One array: DECLARE Names : ARRAY[1:30] OF STRING
2. How do you reach one particular value stored in an array?
A Use the array name with an index, such as Names[5]
B Give every value its own separate variable name
3. Which statement about arrays is TRUE?
A A loop can process every element of an array
B Each element needs its own line of code to be processed
4. A program must store 100 test marks. Which design makes the program shorter?
A 100 variables with 100 INPUT statements
B One array of 100 INTEGER values and a loop
5. The class grows from 30 to 35 students. Which program is easier to change?
A The array version: change the upper bound and the loop limit
B The separate variables version: add five more variables and statements

Match the Pairs

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

Left columnRight column
1DECLARE Names : ARRAY[1:30] OF STRING AName1, Name2, Name3 ... Name30
2FOR i ← 1 TO 30 OUTPUT Names[i] NEXT i BNames[7]
3One name and an index reach each value in an array CWriting one OUTPUT per variable does not use a loop; with an array a FOR loop could output every element in a few lines
4Without arrays you need a separate variable for every value DA loop processes every element of the array using the index
5DECLARE Name1 : STRING DECLARE Name2 : STRING DECLARE Name3 : STRING (repeated for 30 students) EUsing a separate variable for every student makes the program long and hard to change; one array should be used instead
6OUTPUT Name1 OUTPUT Name2 OUTPUT Name3 (repeated 30 times) FOne array stores 30 student names under a single name

Definitions

ARRAY

Fill in the Blanks

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

1.
DECLARE Names : _____[1:30] OF STRING
2.
Names[_] ← "Ava"
3.
FOR Index ← 1 TO 30 OUTPUT Names[_____] NEXT Index
4.
DECLARE Marks : ARRAY[1:30] OF _______
5.
FOR Index ← 1 TO 30 Total ← Total + Marks[Index] ____ Index

Spot the Error

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

Question 1
A Name[1] ← "Ava"
B Name[2] = "Liam"
C Name[3] ← "Mia"
Question 2
A FOR i ← 1 TO 30
B OUTPUT Name
C NEXT i
Question 3
A FOR i ← 1 TO 30
B INPUT Name[i]
C OUTPUT Name[i]
D NEXT j
Question 4
A FOR i ← 1 TO 30
B INPUT Name[i]
C NEXT i
D OUTPUT Name(1)
Question 5
A Count ← 0
B FOR i ← 1 TO 30
C IF Mark[i] >= 50
D Count ← Count + 1
E ENDIF
F NEXT i

Answer Key

Why arrays are used
Section A: Quick Questions
Q1
B — One array: DECLARE Names : ARRAY[1:30] OF STRING
Q2
A — Use the array name with an index, such as Names[5]
Q3
A — A loop can process every element of an array
Q4
B — One array of 100 INTEGER values and a loop
Q5
A — The array version: change the upper bound and the loop limit
Section B: Match the Pairs
Q1
DECLARE Names : ARRAY[1:30] OF STRING → One array stores 30 student names under a single name
Q2
FOR i ← 1 TO 30 OUTPUT Names[i] NEXT i → A loop processes every element of the array using the index
Q3
One name and an index reach each value in an array → Names[7]
Q4
Without arrays you need a separate variable for every value → Name1, Name2, Name3 ... Name30
Q5
DECLARE Name1 : STRING DECLARE Name2 : STRING DECLARE Name3 : STRING (repeated for 30 students) → Using a separate variable for every student makes the program long and hard to change; one array should be used instead
Q6
OUTPUT Name1 OUTPUT Name2 OUTPUT Name3 (repeated 30 times) → Writing one OUTPUT per variable does not use a loop; with an array a FOR loop could output every element in a few lines
Section C: Definitions
Q1
ARRAY — An array stores many values of the same type under one name, so you need one array instead of many variables. An index reaches each value, and loops can process them all.
Section D: Fill in the Blanks
Q1
DECLARE Names : ARRAY[1:30] OF STRING
Q2
Names[5] ← "Ava"
Q3
FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
Q4
DECLARE Marks : ARRAY[1:30] OF INTEGER
Q5
FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index
Section E: Spot the Error
Q1
B — A value is stored in an array element with the assignment arrow ←, not =. It should be Name[2] ← "Liam".
Q2
B — An array name on its own does not reach one value. The index is needed so each pass of the loop displays one element: OUTPUT Name[i].
Q3
D — NEXT must use the same counter variable as the FOR line. The loop counter is i, so the line should be NEXT i.
Q4
D — The index of an array element goes in square brackets. One name plus an index reaches each value: Name[1].
Q5
C — An IF statement must end its condition line with THEN. It should be IF Mark[i] >= 50 THEN, so one loop can check all 30 marks.