Arrays (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

1-Dimensional Arrays

What is an array?

  • An array is an ordered collection of data items stored under one variable name

  • Each item in the array is called an element, and each element can be accessed using an index

  • An array can only store one data type (for example, all integers or all strings)

  • A 1-dimensional (1D) array stores data in a single list

  • Indexes start at 0 in most programming languages such as Python

  • In SQA pseudocode, arrays may start at 0 or 1, always follow the range given in the question

1d-array-example

Concept comparison

Concept

SQA pseudocode

Python

Explanation

Create

DECLARE scores : ARRAY[0:4] OF INTEGER

scores = []

Creates a blank array with 5 elements (indexes 0–4)

Assign values

SET scores TO [12, 10, 5, 2, 8]

scores = [12, 10, 5, 2, 8]

Creates an array called scores with values assigned

Change a value

SET colours[4] TO "Red"

colours[4] = "Red"

Assigns the colour Red to index 4 (the 5th element)

Example in pseudocode

  • Creating a one-dimensional array called array that contains 5 integers

DECLARE array : ARRAY[0:4] OF INTEGER

SET array[0] TO 1
SET array[1] TO 2
SET array[2] TO 3
SET array[3] TO 4
SET array[4] TO 5

SEND array[0] TO DISPLAY      // Output: 1
SEND array[2] TO DISPLAY      // Output: 3

SET array[1] TO 10            // Modify element at index 1
SEND array TO DISPLAY          // Displays [1, 10, 3, 4, 5]

FOR counter FROM 0 TO 4 DO
    SEND array[counter] TO DISPLAY
END FOR

SET length TO 5
SEND length TO DISPLAY         // Output: 5

Example in Python

# Creating a one-dimensional array
array = [1, 2, 3, 4, 5]

# Accessing elements of the array
print(array[0])   # Output: 1
print(array[2])   # Output: 3

# Modifying elements of the array
array[1] = 10
print(array)      # Output: [1, 10, 3, 4, 5]

# Iterating over the array
for element in array:
    print(element)

# Output:
# 1
# 10
# 3
# 4
# 5

# Length of the array
length = len(array)
print(length)     # Output: 5

Worked Example

A teacher records the test scores of 5 pupils in a class.

The data is stored in a 1D array called testScores.

Index

0

1

2

3

4

Value

45

67

89

73

56

(a) Write pseudocode to output the score stored at index 2.

[1]

(b) Write pseudocode to change the value stored at index 4 to 60.

[1]

(c) Write pseudocode to output all scores in the array using a loop.

[2]

Answers

(a) SEND testScores[2] TO DISPLAY [1 mark]

(b) SET testScores[4] TO 60 [1 mark]

(c)

FOR counter FROM 0 TO 4 DO
    SEND testScores[counter] TO DISPLAY
END FOR

[2 marks]

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.