Friday, October 16, 2009

Multi-dimensional Arrays:

An n-dimensional m1 x m2 x…x mn array B[m1][m2]..[mn] is a collection of m1.m2…mn data elements in which element is described by a list of n integers – such as K1, K2,…..Kn – called subscripts, with the property that

LBi£ Ki £ UBi, 1£ i £ n or 0 £ Ki £ mi – 1

The array will be stored in memory in a sequence of memory locations. It will be stored either in row-major order or in column-major order. In row-major order the last subscript varies first (most rapidly), the next to last subscript varies second (less rapidly), and so on. In column-major order, the first subscript varies first (most rapidly), the second subscript second (less rapidly), and so on.

For illustration we will consider a 3-dim array A [7] [5] [3].

Array A contains m1. m2. m3 elements = 7 x 5 x 3 = 105 elements.

If each element uses 4 bytes in memory, then total number of
cell = 105 x 4 = 420 cells. Suppose A is stored in memory at
base address 200.

The address of an element of A, for example A[5][3][2] when it is stored in
row-major order is calculated as follows.

Using row-major order formula for address location

Loc (A [K1][ K2 ] [K3 ]) = Base (A) + W [ (K1 . m2 + K2) m3 + K3 ]

Loc ( A [ 5][3][2] ) = 200 + 4 [ ( 5 x 5 + 3) x 3 + 2] = 200 + 4 [ (28) x 3 + 2]

= 200 + 4 [ 84 + 2] = 200 + 344 = 544

Therefore element A[5][3][2] is located at address 544.

The address of element A[5][3][2] when it is stored in column-major order is calculated as follows.

Loc (A[5][3][2] )

= Base (A) + W [ (K3 m2 + K2) m1 + K1] = 200 + 4 [ (2 x 5 + 3) 7 + 3]

= 200 + 4 [ 91 + 5] = 200 + 384 = 584

No comments:

Post a Comment