Arrays

Arrays - Beginner

Learn how to declare, fill, access and process arrays using CIE pseudocode. Arrays let you store many related values under one name and process them with FOR loops.

19.1 What is an Array?

  • An array is a collection of variables of the SAME type, stored under ONE name.
  • Each element is reached using an INDEX (position number).
  • CIE pseudocode uses 1-based indexing — the first element is at index 1.
  • Arrays let you store many related values without declaring separate variables for each one.
DECLARE Numbers : ARRAY[1:5] OF INTEGER
Numbers[1] <- 10
OUTPUT Numbers[1]

Key idea: Think of an array as a row of labelled boxes. The name tells you which row, and the index tells you which box.

19.2 Declaring and Initializing Arrays

  • Declaration syntax: DECLARE <name> : ARRAY[<start>:<end>] OF <type>
  • The start index is usually 1 in CIE pseudocode.
  • Arrays can hold INTEGER, REAL, STRING, BOOLEAN or CHAR values.

Initializing with a loop (input from user):

DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter number: "
  INPUT Numbers[I]
NEXT I

Initializing with specific values:

DECLARE Days : ARRAY[1:7] OF STRING
Days[1] <- "Monday"
Days[2] <- "Tuesday"
Days[3] <- "Wednesday"
Days[4] <- "Thursday"
Days[5] <- "Friday"
Days[6] <- "Saturday"
Days[7] <- "Sunday"

Exam tip: When a question asks you to “populate” an array, use a FOR loop with INPUT. When the values are fixed (like day names), assign each one explicitly.

19.3 Accessing Array Elements

  • Access an individual element with its index: Numbers[3]
  • Access every element using a FOR loop and a counter variable as the index.
  • OUTPUT Numbers[1] prints the first element.
  • Common operations include printing all elements, finding a specific element and counting elements.
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
FOR I <- 1 TO 5
  OUTPUT Numbers[I]
NEXT I

Common mistake: Writing Numbers(I) with parentheses. CIE uses square brackets — Numbers[I] — for array indexing.

19.4 Input and Output with Arrays

  • Input: loop through the array and INPUT each element by index.
  • Output: loop through the array and OUTPUT each element by index.
  • Use two separate loops so input and output are clearly separated.

Example: input 5 names and display them

DECLARE Names : ARRAY[1:5] OF STRING
DECLARE I : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter name: "
  INPUT Names[I]
NEXT I
OUTPUT "You entered:"
FOR I <- 1 TO 5
  OUTPUT Names[I]
NEXT I

Key idea: The loop counter I doubles as the array index. Each pass of the loop reads or prints exactly one element.

19.5 Basic Array Operations

  • Sum — add every element to an accumulator.
  • Count — increment a counter when an element meets a condition.
  • Find max/min — track the largest or smallest value seen.
  • Search — check whether a value exists in the array.

Example: sum of array elements

DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- 1 TO 5
  OUTPUT "Enter number: "
  INPUT Numbers[I]
  Total <- Total + Numbers[I]
NEXT I
OUTPUT "Sum: ", Total

Find maximum

DECLARE Max : INTEGER
Max <- Numbers[1]
FOR I <- 2 TO 5
  IF Numbers[I] > Max THEN
    Max <- Numbers[I]
  ENDIF
NEXT I
OUTPUT Max

Count greater than 10

DECLARE Count : INTEGER
Count <- 0
FOR I <- 1 TO 5
  IF Numbers[I] > 10 THEN
    Count <- Count + 1
  ENDIF
NEXT I
OUTPUT Count

Common mistake: Initialising Max to 0 when the array may contain only negative numbers. Initialise Max to the first element instead.

19.6 Key Points Summary

Arrays store multiple values of the SAME type under one name.

CIE uses 1-based indexing — the first element is at index 1.

Declare with DECLARE <name> : ARRAY[1:N] OF <type>.

Access elements with <name>[<index>] using square brackets.

Use FOR loops to iterate through arrays.

Common operations: sum, count, find max/min, search.

Array size is fixed at declaration time.

Initialise accumulators (Sum, Count) before the loop.

Exam tip: When a question asks you to process every element, reach for a FOR loop from 1 to N — it covers every index and is the standard CIE pattern.

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

Question 1Multiple Choice

A teacher wants to store the test scores of 30 students. Which is the best approach in CIE pseudocode?

Question 2True / False

The declaration DECLARE Nums : ARRAY[0:9] OF INTEGER uses 1-based indexing.

Question 3Multiple Choice

DECLARE Numbers : ARRAY[1:3] OF INTEGER
Numbers[1] <- 5
Numbers[2] <- 10
Numbers[3] <- 15
OUTPUT Numbers[2]

Question 4Multiple Choice

Which declaration creates a 7-element string array called Days?

Question 5True / False

A single CIE array can store INTEGER values in some positions and STRING values in others.

Question 6Multiple Choice

DECLARE Numbers : ARRAY[1:2] OF INTEGER
DECLARE Total : INTEGER
Total <- 0
Numbers[1] <- 5
Numbers[2] <- 10
Total <- Total + Numbers[1]
Total <- Total + Numbers[2]
OUTPUT Total

Question 7Multiple Choice

Which approach correctly finds the maximum value in an array of integers?

Question 8True / False

The loop FOR I <- 1 TO N iterates exactly N times, covering array indices 1 through N.

Question 9Multiple Choice

DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Count : INTEGER
Count <- 0
Numbers[1] <- 8
Numbers[2] <- 3
Numbers[3] <- 9
Numbers[4] <- 12
Numbers[5] <- 1
FOR I <- 1 TO 5
  IF Numbers[I] > 5 THEN
    Count <- Count + 1
  ENDIF
NEXT I
OUTPUT Count

Question 10True / False

An array index must always be a literal number such as 1 or 5; it can never be a variable.

Question 11Multiple Choice

Which pattern reads 5 numbers from the user into an array called Numbers?

Question 12Multiple Choice

Why must the accumulator Total be initialised to 0 before a sum loop?

Answer all 12 questions to enable submission.