An array is a table of related variables, called elements, of uniform size stored in computer memory.
An array is given a single name and each element’s value is accessed using this arrayname and a subscript.
A subscript is a positive integer value that refers to an element’s position in the array. The subscript is written enclosed in parentheses following the arrayname. Subscripts may be represented in one of three ways:
|
Subscript |
Example |
|
An integer |
Month( 3 ) |
|
An integer variable |
Month( I ) |
|
An integer expression |
Month(I + 1) |
Here is an example of a 12-element array named month:
|
Jan |
Feb |
Mar |
Apr |
May |
Jun |
Jul |
Aug |
Sep |
Oct |
Nov |
Dec |
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
In this example:
In many programming applications that involve large amounts of related data, the computer must be able to store all the related values at one time, rather than one at a time (as is the case with single variable input). For example, suppose we needed to have all the daily high temperatures for a year so we could compute the average temperature. But then also suppose we needed to know how much each day’s temperature deviated from that average.
To do this we need an array of 365 elements in which to place each day’s high temperature. Then we could, through the use of loop processing and a variable subscript, average all 365 values and then go back to the beginning of the array and, one at a time, compare each day’s temperature with that average – a task which would not be very practical without the use of an array.