Arrays
Variables are used for storing one piece of data. Imagine that your system needs to keep track of the ages of 30 people... this would require 30 separate variables. Not necessarily! Arrays can solve this problem by providing a single data structure that can store a number of separate pieces of data.
One dimensional arrays
An array is a data structure that allows multiple, separate pieces of data to be stored name (identifier). Within the array, each item is stored with an address referred to as its index. See below example:
Array name - studentNames
Index |
1 |
2 |
3 |
4 |
5 |
Data |
"Mary" |
"Paul" |
"Ivy" |
"Will" |
"Dan" |
In the above example you can see that the array named "studentNames" contains 5 different names.
Each name is given an index number.
In arrays it is important to note that the index numbers can start at 0 or 1 depending on the system. It is good practice to explicitly state the starting index when declaring an array. CAIE will accept both methods, however they state in their Pseudocode guide that "generally a lower bound of 1 will be used" and therefor we will follow this idea on this page.
Once you have an array that contains data like this you can choose to output certain items by using the name and the desired index number within squared brackets.
arrayname[index]
Example
Each name is given an index number.
In arrays it is important to note that the index numbers can start at 0 or 1 depending on the system. It is good practice to explicitly state the starting index when declaring an array. CAIE will accept both methods, however they state in their Pseudocode guide that "generally a lower bound of 1 will be used" and therefor we will follow this idea on this page.
Once you have an array that contains data like this you can choose to output certain items by using the name and the desired index number within squared brackets.
arrayname[index]
Example
Array output example
When the example code above is executed the output will be:
Ivy
Ivy
Declaring Arrays
The declaration process for arrays can differ slightly between languages but in terms of the CAIE pseudocode, we have been given the following rules to follow:
DECLARE <identifier> : ARRAY [<1>:<n>] of <data type>
If we were to follow this convention to declare our studentNames array, the code would look like this:
DECLARE <identifier> : ARRAY [<1>:<n>] of <data type>
If we were to follow this convention to declare our studentNames array, the code would look like this:
CIE Pseudocode array declaration
Following these declaration rules set out by CAIE means that it is important to choose the correct data type. An array can store data of any type e.g. String, Integer, Real.. but you must identify this at the end of the declaration line.
Vertical Divider