Implementation: Data types and structuresArrays

Data is stored differently depending on its type. Numbers are stored as integers or real numbers, text as string or characters. Lists of the same type of data can be stored in an array.

Part ofComputing ScienceSoftware design and development

Arrays

Arrays let programmers store lists of related data such as a list of scores, a list of first names or a list of cities. Each value in a list is given its own position/index value so it can easily be referred to.

For example, when creating a program for a bowling alley and storing each player’s name you could declare five different variables instead of using an array:

DECLARE PlayerName1 AS STRING INITIALLY “ “
DECLARE PlayerName2 AS STRING INITIALLY “ “
DECLARE PlayerName3 AS STRING INITIALLY “ “
DECLARE PlayerName4 AS STRING INITIALLY “ “
DECLARE PlayerName5 AS STRING INITIALLY “ “

However, this is inefficient and means that programmers can spend a lot of time declaring variables. Instead an array could be used. An array needs a name and size, so that the computer knows how many memory locations to set aside for the array.

DECLARE PlayerName AS ARRAY OF STRING INITIALLY [“ “]*5

This line of code creates an array called PlayerName with five elements (five places to store data). This single line of code sets aside five locations in memory that can be accessed using the identifiers listed below:

PlayerName[0]
PlayerName[1]
PlayerName[2]
PlayerName[3]
PlayerName[4]

Another array could be used to store the player scores but this array would be an array of integers rather than an array of strings.

DECLARE Score1PlayerName AS ARRAY OF INTEGER INITIALLY [0]*5
Ten pin bowling scores displayed using arrays, player name is array one and first score is array two