Problem Statement - Finding relative address in 2D Array
T[20][50] is a two dimensional array, which is stored in the memory along the row with each of its element occupying 4 bytes, find the address of the element T[15][5], if the element T[10][8] is stored at the memory location 52000.
Solution
TC++ #6167
Loc(T[I][J])
=BaseAddress + W [( I – LBR)*C + (J – LBC)]
(where
W=size of each element = 4 bytes,
R=Number of Rows=20, C=Number of Columns=50)
Assuming LBR = LBC = 0
LOC(T[10][8])
52000 = BaseAddress + W[ I*C + J]
52000 = BaseAddress + 4[10*50 + 8]
52000 = BaseAddress + 4[500 + 8]
52000 = BaseAddress + 4 x 508
BaseAddress = 52000 2032
= 49968
LOC(T[15][5])= BaseAddress + W[ I*C + J]
= 49968 + 4[15*50 + 5]
= 49968 + 4[750 + 5]
= 49968 + 4 x 755
= 49968 + 3020
= 52988
OR
Loc(T[I][J])
=ReferenceAddress + W [( I – LR)*C + (J – LC)]
(where
W=size of each element = 4 bytes,
R=Number of Rows=20, C=Number of Columns=50)
ReferenceAddress= Address of given cell T[10][8]=52000
LR = Row value of given cell = 10
LC = Column value of given cell = 8
LOC(T[15][5])= LOC(T[10][8]) + 4[(15-10)*50 + (5-8)]
LOC(T[15][5]) = 52000 + 4[5*50 + (-3)]
= 52000 + 4[250-3]
= 52000 + 4 x 247
= 52000 + 988
= 52988