Reading values into an array with INPUT in a loop

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

Reading values into an array with INPUT in a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores a value typed by the user into element Index of the array StudentNames?
A INPUT StudentNames
B INPUT StudentNames[Index]
2. A loop runs FOR Index ← 1 TO 5 with INPUT Marks[Index] inside it. What happens?
A Five values are stored in Marks[1] to Marks[5]
B The same element is overwritten five times
3. Where should INPUT Marks[Index] be placed to fill a whole array?
A After NEXT Index, outside the loop
B Inside the FOR loop, before NEXT Index
4. Which is the better way to ask for each mark?
A OUTPUT "Enter mark ", Index followed by INPUT Marks[Index]
B INPUT Marks[Index] with no OUTPUT prompt before it
5. Marks is declared as ARRAY[1:3] OF INTEGER. Which input is suitable?
A The user types 72
B The user types seventy-two

Match the Pairs

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

Left columnRight column
1INPUT StudentNames[Index] AFOR Index ← 1 TO 30 INPUT Marks[Index] NEXT Index
2FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index BOUTPUT "Enter mark ", Index INPUT Marks[Index]
3Show a prompt before each INPUT so the user knows which value to type, then store it in the next element CReads five values from the user and stores them in elements 1 to 5 of Marks, one per repeat of the loop
4A loop with a changing Index fills an INTEGER array of marks, so every element gets its own value from the user DINPUT names the whole array without an index, so it does not say which element stores the value. It should be INPUT Marks[Index].
5FOR Index ← 1 TO 5 INPUT Marks NEXT Index EStores the value the user types into the element of StudentNames at the position held in Index
6FOR Index ← 1 TO 5 INPUT Marks[1] NEXT Index FThe index is fixed at 1, so every value typed overwrites element 1 and the other elements stay empty. It should be INPUT Marks[Index].

Definitions

INPUT Name[Index]

Fill in the Blanks

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

1.
FOR Index ← 1 TO 5 _____ StudentNames[Index] NEXT Index
2.
FOR Index ← 1 TO 5 INPUT StudentNames[_____] NEXT Index
3.
DECLARE Marks : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 5 INPUT _____[Index]
4.
DECLARE StudentNames : ARRAY[1:30] OF STRING FOR Index ← 1 TO __ INPUT StudentNames[Index]
5.
FOR Index ← 1 TO 5 ______ "Enter a name: " INPUT StudentNames[Index]

Spot the Error

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

Question 1
A FOR Index ← 1 TO 5
B INPUT StudentNames
C NEXT Index
Question 2
A FOR Index ← 1 TO 5
B OUTPUT "Enter a mark
C INPUT Marks[Index]
D NEXT Index
Question 3
A FOR Index ← 1 TO 3
B INPUT Marks(Index)
C OUTPUT Marks[Index]
D NEXT Index
Question 4
A FOR Index ← 1 UNTIL 5
B INPUT Marks[Index]
C NEXT Index
Question 5
A FOR Index = 1 TO 4
B OUTPUT "Enter name"
C INPUT Names[Index]
D NEXT Index

Answer Key

Reading values into an array with INPUT in a loop
Section A: Quick Questions
Q1
B — INPUT StudentNames[Index]
Q2
A — Five values are stored in Marks[1] to Marks[5]
Q3
B — Inside the FOR loop, before NEXT Index
Q4
A — OUTPUT "Enter mark ", Index followed by INPUT Marks[Index]
Q5
A — The user types 72
Section B: Match the Pairs
Q1
INPUT StudentNames[Index] → Stores the value the user types into the element of StudentNames at the position held in Index
Q2
FOR Index ← 1 TO 5 INPUT Marks[Index] NEXT Index → Reads five values from the user and stores them in elements 1 to 5 of Marks, one per repeat of the loop
Q3
Show a prompt before each INPUT so the user knows which value to type, then store it in the next element → OUTPUT "Enter mark ", Index INPUT Marks[Index]
Q4
A loop with a changing Index fills an INTEGER array of marks, so every element gets its own value from the user → FOR Index ← 1 TO 30 INPUT Marks[Index] NEXT Index
Q5
FOR Index ← 1 TO 5 INPUT Marks NEXT Index → INPUT names the whole array without an index, so it does not say which element stores the value. It should be INPUT Marks[Index].
Q6
FOR Index ← 1 TO 5 INPUT Marks[1] NEXT Index → The index is fixed at 1, so every value typed overwrites element 1 and the other elements stay empty. It should be INPUT Marks[Index].
Section C: Definitions
Q1
INPUT Name[Index] — Inside a FOR loop, INPUT Name[Index] stores each value the user types into the next array element, because Index changes every time the loop repeats.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 5 INPUT StudentNames[Index] NEXT Index
Q2
FOR Index ← 1 TO 5 INPUT StudentNames[Index] NEXT Index
Q3
DECLARE Marks : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 5 INPUT Marks[Index]
Q4
DECLARE StudentNames : ARRAY[1:30] OF STRING FOR Index ← 1 TO 30 INPUT StudentNames[Index]
Q5
FOR Index ← 1 TO 5 OUTPUT "Enter a name: " INPUT StudentNames[Index]
Section E: Spot the Error
Q1
B — The INPUT has no index, so it does not say which element stores the value. It should be INPUT StudentNames[Index] so each value goes into the next element.
Q2
B — The prompt text is missing its closing quote mark. It should be OUTPUT "Enter a mark" so the prompt displays before each INPUT.
Q3
B — Array indexes use square brackets in IGCSE pseudocode, not round brackets. It should be INPUT Marks[Index].
Q4
A — A count-controlled loop uses the keyword TO, not UNTIL. It should be FOR Index ← 1 TO 5.
Q5
A — The loop counter must be given its start value with the assignment arrow ←, not =. It should be FOR Index ← 1 TO 4.