Data structures - arrays
Sometimes a programmer needs to store a lot of related dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions.. For example, a game might record the scores achieved by players.
One way to do this would be to declare a variableA memory location within a computer program where values are stored. for each score. So for ten scores, the game program would require ten variables:
score1
score2
score3
And so on, up to score10
While certainly possible, this is not a practical method of recording such data. Suppose the program needed to record 100 scores? 100 variables would be required!
A better method to solve the problem above is to use an arrayA set of data values of the same type, stored in a sequence in a computer program. Also known as a list. . An array is a data structure that holds similar, related data. An array is like a collection of boxes, each of which is called an elementAn individual component of an array.. Each element has a position in the array, and can hold a value. The data in an array must all be of the same data typeIn computer programming, data is divided up and organised according to type, eg numbers, characters and Boolean..
This way, all data is stored under one identifierA name given to a part of a program, such as a variable, constant, function, procedure or module.. For example, the array called 鈥渟core鈥 could contain the following information:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
100 | 110 | 85 | 80 | 92 | 72 | 66 | 98 | 100 | 120 |
0 | 100 |
---|---|
1 | 110 |
2 | 85 |
3 | 80 |
4 | 92 |
5 | 72 |
6 | 66 |
7 | 98 |
8 | 100 |
9 | 120 |
Declaring an array
Before an array can be used, it must be declared. To declare an array a programmer gives it at least two properties:
- an identifier
- a size - the number of elements it will hold
For example, in Visual Basic:
Dim score(9) As Integer
- would declare an array with ten elements (zero to nine), where each element would store an integerA whole number - this is one data type used to define numbers in a computer program. Integers can be unsigned (represent positive numbers) or signed (represent negative or positive numbers)..
Once declared, the name and structure of an array cannot be changed.
It is possible to declare an array without setting the size first. This is called a dynamic arrayAn array where the size is not declared at the start. Instead, each time a new item is added, the size grows. . To use a dynamic array, data must be added to the end of the array, or a new size set each time more data is added.
In pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding., declaring an array before use is not needed.
Assigning values to an array
Values are assigned to an element in an array by referring to the element's position. For example:
score[0] 鈫 100
- would assign the value 100 to the first element in the array
Values in elements can be overwritten at any point, simply by assigning another value to that element.
Retrieving values from an array
Values are retrieved from an element in the array by again referring to the element's position, eg:
OUTPUT score[7]
- would display the eighth value held in the array. In the above example, the value would be 98.
Two-dimensional arrays
A two-dimensional array can hold more than one set of data. This type of array is like a table, with data held in rows and columns.
The following array would hold ten scores for two players. The first player (0) has data stored in the first row. The second player (1) has data stored in the second row.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 100 | 110 | 85 | 80 | 92 | 72 | 66 | 98 | 100 | 120 |
1 | 90 | 99 | 102 | 88 | 78 | 100 | 67 | 120 | 88 | 105 |
0 | |
0 | 100 |
1 | 110 |
2 | 85 |
3 | 80 |
4 | 92 |
5 | 72 |
6 | 66 |
7 | 98 |
8 | 100 |
9 | 120 |
1 | |
0 | 90 |
1 | 99 |
2 | 102 |
3 | 88 |
4 | 78 |
5 | 100 |
6 | 67 |
7 | 120 |
8 | 88 |
9 | 105 |
A two-dimensional array is declared using two values - the number of rows and the number of columns. For example:
array score [1,9]
- would give an array with two rows and ten columns
Data is assigned or retrieved by referring to an element's row and column number. For example:
score[0,1] 鈫 110
print(score[1,4])
- would display the score 78