Array Addressing
Row Major and Column Major Order
Address Calculation in 1-D Array
• Memory address calculation Address of an element of an array say A[ I ] is calculated using the following
formula:
Address of A [ I ] = B + W * ( I- LB ) Where,
B = Base address
W = Storage Size of one element stored in the array (in byte)
I = Subscript of the element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified, assume 0 (zero)
• Example:
• Given the base address of an array B[1300..1900] as 1020 and the size of each element is 2 bytes in the
memory. Find the address of B[1700].
Ans: The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of A [ I ] = B + W * ( I -LB ), each element’s size
= 1020 + 2 * (1700-1300)
= 1020 + 2 * 400 = 1820
Address Calculation in 2-D Array
• A is a 2D array with dimensions M1 X M2. So that here the total number of elements
is equal to M1 X M2. The address should be calculated using one of the two methods:
A. Row-by-Row
• Location (A [j, k]) = Base (A) + w [N (j-1) + (k -1)]
• Here, j and k indicate the index value of the element whose address you want to search.
• Base (A) is the starting address of the given array. W is the memory word required to store given
array’s element. The array is of size M(rows) X N(columns).
B. Column-by-Column
• Location (A [j, k]) = Base (A) + w [M (k-1) + (j -1)]
• Here, j and k indicate the index value of the element whose address you want to search.
• Base (A) is the starting address of the given array. W is the memory word required to store given
array’s element. Here, array is the of size M(rows) X N(columns)
Address Calculation in 2-D Array
• Examples
• Consider 30 X 4 2D array and the base address is 200 and 1 word per memory location. Find
out the address of A (15, 3). Given Base (A) = 200, M =30, N=4, j=15, k=3, w=1
A. Row-by-row
Location (A [j, k]) = Base (A) + w [N (j-1) + (k -1)]
Location (A [15, 3]) = 200 + 1 [4 (15 - 1) + (3 - 1)]
= 200 + 1 [56 + 2] = 258
B. Column-by-column
Location (A [j, k]) = Base (A) + w [M (k-1) + (j -1)]
Location (A [15, 3]) = 200 + 1 [30 (3 – 1) + (15 – 1)]
= 200 + 1 [30 (2) + 14] = 274
Address Calculation in 3-D Array
• Suppose A is a 3D array of size M1 X M2 X M3. Total number of elements
are M1 X M2 X M3. In 3D array lower bound (LB) is not always zero (0). It
can be any number. Base address of any row of matrix is called effective
address.
• A [2:11, 3:15] indicates that A is a 2D array with lower bound (LB) of row is
2 and Upper Bound (UB) of row is 11. In the same way lower bound (LB) of
column is 3 and upper bound (UB) of column is 15.
Address Calculation in 3-D Array
• Length of each dimension is equal to Li = UB – LB +1
• Lower bound (LB) may not always start with zero (0). So, we have to
calculate effective address of each index.
• Effective Address (Ei) = Ki - LB
• Real address of any element is calculated as follows,
• Row-Major: Base (A) + W [(E1L2 + E2) L3 + E3]
• Column-Major: Base (A) + W [(E3L2 + E2) L1 + E1]
Address Calculation in 3-D Array
• Example
Suppose multidimensional arrays A and B are declared using A [-2:2,
2:22] and B [1:8, -5:5, -10:5].
Here A is a 2-D array while B is a 3-D array
1. Find out the length of each dimension and the number of elements in
arrays A and B.
2. Consider B [3, 3, 3] elements in array B, find effective addresses E1, E2
and E3. Also, find the real address of these elements. (Assume the base
address of array B is 400, and W is 4)
Address Calculation in 3-D Array
Answer:
For array A
L1 = UB –LB +1 L2 = UB –LB +1
= 2 – (-2) +1 = 5 = 22 – 2 + 1 = 21
For array B
L1 = UB –LB +1 L2 = UB –LB +1 L3 = UB –LB +1
=8–1+1 = 5 – (-5) +1 = 5 – (-10) +1
=8 = 11 = 16
Total number of elements in array A is L1 X L2 = 5 X 21 = 105.
Total number of elements in array B is: L1 X L2 X L3 = 8 X 11 X 16 = 1408.
Address Calculation in 3-D Array
Answer:
Ki = 3, 3, 3 therefore K1 = 3, K2 = 3 and K3 = 3
LB1= 1, LB2 = -5 LB3 = -10
E1 = K1 – LB1 = 3 – 1 = 2
E2 = K2 – LB2 = 3- (-5) = 8
E3 = K3 – LB3 = 3 – (-10) = 13
The real address for Row Major: The real address for Row Major:
= Base (A) + W [ (E1L2 + E2) L3 + E3] = Base (A) + W [(E3L2 +E2) L1 + E1]
= 400 + 4 [(2 (11) + 8) 16 + 13] = 400 + 4 [(13(11) + 8) 8 + 2]
= 400 + 1972 = 400 + 4840
= 2372 = 5240