Address Calculation
Address Calculation
This is the reason why, address calculation is essential to work with data
values.
Types of Array
1.Single Dimensional (1-D)
2.Double dimensional(2-D)
Number of bytes occupied by an element in the memory as per its data typ
is below:
Byte=8 bits
boolean : 1 byte
short: 2 bytes
char type: 2 bytes
int type: 4 bytes
float type: 4 bytes
double : 8 bytes
long type: 8 bytes
Address Calculation in Single Dimensional Array
Let the array be Arr. The address of an element Arr[I] can be calculated by using the following formula:
Address= B + W(I - LB)
where.
W = Word width (Number of bytes an element occupies in the memory).
I = index/Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Example1:
Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2 bytes in the memory.
Find the address of B[1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of B [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
Example 2:
A single dimensional array Arr contains 10 integer numbers.If the base address of the array is 1000, find the address o
sixth and eighth elements of the array.
Solution:B =1000
I= 5 (Subscript of the 6th element is 5 because lowest cell number of the array is 0)
W = 4 (Word width of integer number is 4)
L = 0 (Lowest cell number is taken to be 0 by default)
Address of A [ 2 ][ 2 ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
Solution:I=2
J=2
Lr=0
Lc=0
N=4
B=?
2000=B+4[4(2-0)+(2-0)]
2000= B+4[4*2+2]
2000=B+40
B=2000-40
=1960
A matrix N[11][8] is stored in the memory with each element requiring 2 bytes of storage. If the base
address at N[2][3] is 2140, find the address of N[7][5] when the matrix is stored in Row Major Wise.
Given B = 2140
W = 2 bytes
i = 7, j = 5, Lr = 2, Lc = 3, Ur = 11, Uc = 8
n = Uc – Lc + 1 = 8 – 3 +1 = 6
N [i][j] = B + W [n (i – Lr) + (j – Lc)]
N[7][5] = 2140 + 2 [6 (7 – 2) + (5 – 3)]
= 2140 + 2 [6 (5) + 2]
= 2140 + 2 [30 + 2]
= 2140 + 2 (32)
= 2140 + 64
N[7][5] = 2204