Initializing A Multi-Dimensional Array
A question that often arises when dealing with arrays that have more than one subscript is: "In what order are the array's elements stored?" For example, suppose we initialize a 3-dimensional array as follows:
Int array[4][3][2]={0,1,2,3,4,5,6,7, ... etc.}
Into which elements of the array do each of the values (0-23) go? Well,
one sure way to find out is to try it for ourselves and see what happens:
Int array[4][3][2]={ 0, 1, 2, 3, 4, 5, \
6, 7, 8, 9,10,11, \
12,13,14,15,16,17, \
18,19,20,21,22,23}
Do i=0,3
Do j=0,2
Do k=0,1
output (array[i][j][k], 1)
Loop
Loop
Loop
When we run the program, it
displays:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23In other words, we see that:
0 was stored into array[0][0][0]
1 was stored into array[0][0][1]
2 was stored into array[0][1][0]
3 was stored into array[0][1][1]
4 was stored into array[0][2][0]
5 was stored into array[0][2][1]
6 was stored into array[1][0][0]
7 was stored into array[1][0][1]
8 was stored into array[1][1][0]
--- etc. ---
From this, we can infer the general rule:
|
When storing data values into consecutive elements of an array, |
[
Webmaster |
FAQ's |
Home Page |
Contact Us ]