Parallel Arrays

There is nothing new about ‘parallel’ arrays. 

Remember that a 1-D array can only hold one instance of a variable at any given element so for example if storing 4 names.

e.g.

Names = ['John', 'James, 'Mary', 'Laura']

An array holding 3 names

But what if we are storing names and ages? We can store the names in an array called Names and the ages in an array called Ages

Such as shown to the right

These are known as parallel arrays 

as they are arranged in parallel with each other

You occasionally did this in National 5 but will be used more often at Higher when reading files.


Two parallel arrays holding names and ages.

We know that the data in the names array is arranged in parallel with the ages array so….

names[0]  is ages[0] years old

John is 23 years old

names[1]  is ages[1] years old

James is 22 years old

names[2]  is ages[2] years old

Mary is 20 years old

names[3]  is ages[3] years old

Laura is 19 years old

The name and age of the first person is at position [0]

Traversing an Array

When dealing with an array, traversing an array means to go through the array one element at a time. You will usually need to use a fixed loop to process each element at a time. Remember an array is like a filing cabinet you can only use one element at a time.

Inputting into an array

A standard piece of code input into an array would be:

Displaying from an array

A standard piece of code to display from an array would be:

There is also a quick N5 reminder tutorial video below.

N5 Array Reminder.mp4