Array Address Calculation
• This is the technique used to find the memory address of any cell in
an array.
• Requirements for doing the array address calculation
• The datatype of the array or storage size
• The order in which the data is stored
• A reference address of any cell
• Total number of rows and columns in the array
Array Address Calculation
A char array A [4][4] stored in Row major has a starting address 2000 at 0,0.
0 1 2 3
0,0 0,1 0,2 0,3
0 2000 2002 2004 2006
1,0 1,1 1,2 1,3
1 2008 2010 2012 2014
2,0 2,1 2,2 2,3
2 2016 2018 2020 2022
3,0 3,1 3,2 3,3
3 2024 2026 2028 2030
Array Address Calculation
An array A [4][4] stored in Column major has a starting address 100 at 0,0 and
requires 5 bytes for storage.
0 1 2 3
0,0 0,1 0,2 0,3
0 100 120 140 160
1,0 1,1 1,2 1,3
1 105 125 145 165
2,0 2,1 2,2 2,3
2 110 130 150 170
3,0 3,1 3,2 3,3
3 115 135 155 175
Array Address Calculation Formula
• Row Major : Address of A[i][j] = B + W * [ n * ( i – Lr ) + ( j – Lc)]
• Column Major : Address of A[i][j] = B + W *[ ( i – Lr ) + m *( j – Lc)]
B : base address
W : Storage size of element in bytes
i : row subscript of element whose address is to be found
j : column subscript of element whose address is to be found
Lr : start row / lower limit of row (if not given assume as 0)
Lc : start column / lower limit of column (if not given assume as 0)
m : no. of rows in array n : no. of columns in array
Array Address Calculation Formula
• Usually the number of rows and columns is provided but in some
cases it may be given a range
A[Lr ………. Ur , Lc ……… Uc]
then m (no. of rows ) = (Ur – Lr) + 1
n (no. of columns ) = (Uc – Lc) + 1
Array Address Calculation Worksheet : Example 1
An array X[10][5] is stored in memory with each element requiring 2 bytes of
storage. If the first element X[0][0] is stored at location 1250, calculate the
location of X[5][3] when the array is stored row major wise.
Row Major : Address of X[i][j] = B + W * [ n * ( i – Lr ) + ( j – Lc)]
X[5][3] = 1250 + 2 * [5 * (5 – 0) + (3 – 0)]
= 1250 + 2 * [ 5 * 5 + 3]
= 1250 + 2 * [25 + 3]
= 1250 + 2 * 28
= 1250 + 56
= 1306
Array Address Calculation Worksheet : Example 2
In an array of real numbers A[25][25], the base A[1][1] is stored in location
1000. Find the address of A[12][12] when the array is stored row major wise.
Assume storage of each element is 4 bytes.
Row Major : Address of A[i][j] = B + W * [ n * ( i – Lr ) + ( j – Lc)]
A[12][12] = 1000 + 4 * [25 * (12 – 1) + (12 – 1)]
= 1000 + 4 * [ 25 * 11 + 11]
= 1000 + 4 * [275 + 11]
= 1000 + 4 * 286
= 1000 + 1144
= 2144
Array Address Calculation Worksheet : Example 3
An array A[-4….6,-2….12] stores elements in row major wise, with the address
A[2][3] as 4142. If each element requires 2 bytes of storage, find the base
address.
Row Major : Address of A[i][j] = B + W * [ n * ( i – Lr ) + ( j – Lc)]
A[2][3] = B + 2 * [15 * (2 – ( – 4) + (3 – ( – 2)]
4142 = B + 2 * [ 15 * 6 + 5]
4142 = B + 2 * [90 + 5]
4142 = B + 2 * 95
4142 = B + 190
4142 – 190 = B B = 3952
Array Address Calculation Worksheet : Example 4
A square matrix M[][] of size 10 is stored in the memory with each element
requiring 4 bytes of storage. If the base address at M[0][0] is 1840, determine
the address at M[4][8] when the matrix is stored in row major wise.
Row Major : Address of M[i][j] = B + W * [ n * ( i – Lr ) + ( j – Lc)]
M[4][8] = 1840 + 4 * [10 * (4 – 0) + (8 – 0)]
= 1840 + 4 * [ 10 * 4 + 8]
= 1840 + 4 * [40 + 8]
= 1840 + 4 * 48
= 1840 + 192
= 2032
Array Address Calculation Worksheet : Example 5
A character array B[7][6] has a base address 1046 at 0,0. Calculate the address
at B[2][3] if the array is stored column major wise. Each character requires 2
bytes of storage.
Column Major : Address of A[i][j] = B + W *[ ( i – Lr ) + m *( j – Lc)]
B[2][3] = 1046 + 2 * [(2 – 0) + 7 * (3 – 0)]
= 1046 + 2 * [ 2 + 7 * 3]
= 1046 + 2 * [2 + 21]
= 1046 + 2 * 23
= 1046 + 46
= 1092
Array Address Calculation Worksheet : Example 6
Each element of an array A[20][10] requires 2 bytes of storage. If the address of
A[6][8] is 4000, find the base address at A[0][0] when the array is stored as row
major wise.
Row Major : Address of A[i][j] = B + W * [ n * ( i – Lr ) + ( j – Lc)]
A[6][8] = B + 2 * [10 * (6 – 0) + (8 – 0)]
4000 = B + 2 * [ 10 * 6 + 8]
4000 = B + 2 * [60 + 8]
4000 = B + 2 * 68
4000 = B + 136
4000 – 136 = B B = 3864