Arrays |
|
One-dimensional array We have seen that an
array is a data structure consisting
of a list data items of the same data type.
Each of the data items is identified by the
name of the array and a subscript.
Example :
Sales() - the sales figures for Tom, a computer salesman
for each month of the year...
|
|
|
Sales |
Tom |
Jan |
£2300 |
Feb |
£1850 |
Mar |
£3200 |
Apr |
£2980 |
... |
... |
Sales(0) = £2300
Sales(3) = £2980
|
Note : The
cells with a grey background are for annotation purposes...
the actual array is in the green cells. |
|
|
Two-dimensional arrays A two
dimensional array needs two subscripts
to identify each element.
Example :
Sales() - the sales figures for a
number of different salesmen (Tom, Dick, Harry, ...) for each month of
the year.
Sales |
Tom |
Dick |
Harry |
... |
Jan |
£2300 |
£5400 |
£1200 |
... |
Feb |
£1850 |
£4120 |
£1980 |
... |
Mar |
£3200 |
£4400 |
£1750 |
... |
Apr |
£2980 |
£4390 |
£2160 |
... |
... |
... |
... |
... |
... |
Sales(0,0) = £2300
Sales(3,2) = £2160
|
You can think of a 2-dimensional array as a
spreadsheet with rows and columns...
...the row subscript always comes before the column
subscript. |
|
|
Three-dimensional arrays Arrays
with dimensions more than two are common in computer programming.
Generally, an n-dimensional array needs n subscripts to identify each
element of the array.
Example :
Sales() - the sales figures for a
number of different salesmen (Tom, Dick, Harry, ...) for each month of
the year, for a number of years.
|
|
|
...
Sales(0,0,0) = £2300
Sales(3,2,2) = £1160 |
|
 |
|