Addressing An Element Of An Array At A Particular Index In 2D Array

let A[3][4]

2d row index and column index

While storing the elements of 2D array in memory, these are allocated contiguous memory locations. Therefore 2D array must be linearized so as to enable this storage.

Two ways to linearize it

Row Major

Column Major Row Major and column major

Row Major System:

The address of a location in row major system

Address of A[i][j]=B+W*[N*(i-Lr)+(j-Lc)]

Column Major System :

Address of a location in Column Major System

Address of A[i][j] Column Major wise=B+W*[(i-Lr)+M*(j-Lc)]

Where

B= base address
i= row Subscript of element whose address to be found
W= Storage size of an element
Lr= lower limit of RoW / start row index of matrix, if not given then assume zero (0)
Lc= lower limit of Column / start column index of matrix, if not given then assume 0(Zero)
M= number of rows of the given matrix
N= number of column of the given matrix

** usually number of rows and column of a matrix are given like
A[10][15], A[5][2] but if it is given as

A[Lr......Ur][Lc.......Uc]


So in this case number of rows and columns will be calculated as

rows(M)= (Ur-Lr)+1

column(N)=(Uc-Lc)+1


Example:

An array X[-15.......10,15.....40] required One byte of the storage and the beginning location is 1500 so determine the location of X[15][20]

Solution: We have to find the number of rows and column of matrix x

M=(Ur-Lr)+1 => [10-(-15)]+1=> 26

N=(Uc-Lc)+1=> [40-15]+1 => 26

1) Row Major wise calculation of X[15][20]

B=1500, W=1 byte, I=15, J=20, Lr=-15, Lc=15,N=26

Address of A[I][J]= B+W*[N*(i-Lr)+(J-Lc)]

= 1500+1*[2*(15-(-15))+(20-15)]

=1500+1*[26*30+5]

=1500+785=2285

2) Column Major wise calculation of X[15][20]

B=1500, W=1; i=15,J=20,Lr=-15,Lc=15, M=26

X[15][20]= B+W*[(i-Lr)+M*(j-Lc)]

=1500+1*[(15-(15)+26*(20-15))]

=1500+1*[30+26+5]

=1500+1*(160)

=1660

09- Array- Addressing an Element in 2-D-Array- C Programming Language






    CLICK HERE TO Download This PDF NOTES




Facebook Likes

Youtube