|
A two-dimensional array has two subscripts
Example:
The three monthly sales totals
for six salesmen are set up in a two-dimensional array called 'SALES'.
| |
January |
February |
March |
| John
Sykes |
£450 |
£320 |
£520 |
| Linda
Judge |
£180 |
£220 |
£460 |
| Sam
Good |
£620 |
£650 |
£710 |
| Lynn
Horton |
£460 |
£420 |
£380 |
| Jim
Talbot |
£120 |
£210 |
£190 |
| Lucy
King |
£420 |
£380 |
£550 |
There are 6 rows and 3 columns. (The grey
headings are not part of the array - they are headings for us to
understand the meaning of the array)
SALES[3,2] would be '£650' (Row 3, Column 2).
NB : In Pascal, a variable declaration
would have to be made as follows :
var SALES :
Array[1..6,1..3] of integer;
When processing a two-dimensional array, it
usually involves a nested
loop (a loop inside another loop).
The following Pascal procedure would display the elements of the
sales figures array..
| procedure
DisplayArray; |
| var i, j : integer; |
| begin |
| |
for i := 1 to 6 do |
| |
begin |
|
| |
|
for j := 1 to 3
do |
| |
|
|
write(Sales[i,j] ); |
| |
|
writeln; |
| |
end |
|
| end; |
|