Saturday, September 29, 2012

ADDRESS CALCULATION FOR TWO DIMENSIONAL ARRAY


When lower bond is not given.

The computer keeps track of Base (BA), 
the address of the first element A[0][0] of A[M][N], 
and computes address Loc (A[I][J]) of A[M][N] using the formula

Address can be calculated as Column major order:-

Loc (A[I][J] ) = Base (BA) + W [M x J + I ]

Address can be calculated as Row major order:-

Loc (A [I][J]) = Base (BA) + W [N x I + J ]

W denotes the size, i.e; number of bytes per data element of the array A,
 M is total numbers of rows, and N is total number of columns in the array.

When lower bond is given.

Address can be calculated as Row major order:-

Address of A[ I ][ J ]th element = BA + [ N * ( I - LBR) + ( J - LBC) ] * W

Where BA = Base address
W = Number of bytes occupied by each element
N = Number of columns

Address can be calculated as Column major order:-

Address of a[ I ][ J ]th element = BA + [ ( I - LBR) + M * ( J - LBC) ] * N

Where BA = Base address
W = Number of bytes occupied by each element
M = Number of rows

In case of C language 
LBR (Lower bound row) = 0
LBC (Lower bound column) = 0

e.g. Suppose element of array 
A[4][5] occupies 4 bytes, and the address of the 1st element is 49.  Find the address of the element A(4,3) when the storage is row major.


                                                     = BA + [n * (i - LBR) + (j - LBC)] * w
                                                     = 49 + [5 * (4 – 0) + (3 - 0)] * 4
                                                     = 49 + [23] * 4
                                                     = 49 + 92
                                                     = 141

4 comments:

  1. Address can be calculated as Column major order:-

    Address of a[ I ][ J ]th element = BA + [ ( I - LBR) + M * ( J - LBC) ] * W

    ReplyDelete
  2. it was really helpful . thank you

    ReplyDelete