Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views7 pages

Address Calculation 2D Solved Problms_OK

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Address Calculation 2D Solved Problms_OK

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Address Calculation in Double (Two) Dimensional Array: While storing the elements of a 2-D array in

memory, these are allocated contiguous memory locations. Therefore, a 2-D array must be linearized so
as to enable their storage.

There are two alternatives to achieve linearization: Row-Major and Column-Major.

Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given: (1) Row Major
System (2) Column Major System

Row Major System: The address of a location in Row Major System is calculated using the following
formula:

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

Column Major System: The address of a location in Column Major System is calculated using the
following formula:

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 is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix Important: Usually number of rows and columns of a
matrix are given ( like A[20][30] or A[40][60] ) but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc].
In this case number of rows and columns are calculated using the following methods:
Number of rows (M) will be calculated as = (Ur – Lr) + 1 Number of columns (N) will be calculated as =
(Uc – Lc) + 1 And rest of the process will remain same as per requirement (Row Major Wise or Column
Major Wise).

Examples: Q 1. An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location
is 1500 determine the location of X [15][20]. Solution: As you see here the number of rows and columns
are not given in the question. So they are calculated as:

Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26 Number or columns say N = (Uc – Lc) + 1 =
[40 – 15)] +1 = 26

(i) Column Major Wise Calculation of above equation The given values are: B = 1500, W = 1
byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26 Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J
– Lc ) ] = 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5] = 1500 + 1 * [160]
= 1660 [Ans]
(ii) Row Major Wise Calculation of above equation The given values are: 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* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5] =
1500 + 785 = 2285 [Ans]
Array Memory Address Calculation : Year 2024
An array ARR[-5…15, 10…20] stores elements in Row Major Wise with each
element requiring 2 bytes of storage. Find the address of ARR[10][15] when the
base address is 2500.
C = 20 – 10 + 1 = 11
Address of ARR[10][15] = B + W[C(I – Lr) + (J – Lc)]
= 2500 + 2[11(10 – (-5)) + (15 – 10)]
= 2500 + 2[11(15) + 5]
= 2500 + 2[165 + 5]
= 2500 + 2[170]
= 2500 + 340
= 2840

Year 2023 : A matrix M[-6…10, 4…15] is stored in the memory with each
element requiring 4 bytes of storage. If the base address is 1025, find the
address of M[4][8] when the matrix is stored in column major wise.
B = 1025, Lr = -6, Lc = 4, R = 10 – (-6) + 1 = 10 + 6 + 1 = 17
I = 4, J = 8, W = 4
Address of M[4][8] = B + W[R(J – Lc) + (I – Lr)]
= 1025 + 4[17(8 – 4) + (4 – (-6))]
= 1025 + 4[17 × 4 + (4 + 6)]
= 1025 + 4[68 + 10]
= 1025 + 4 × 78
= 1025 + 312
= 1337

Year 2022 : A matrix MAT[10][15] is stored in the memory in row major wise
with each element requiring 2 bytes of storage. If the base address at MAT[1][2]
is 2215, then the address of MAT[3][7] will be?
B = 2215, Lr = 1, Lc = 2, C = 15, I = 3, J = 7, W = 2
Address of MAT[3][7] = B + W[C(I – Lr) + (J – Lc)]
= 2215 + 2[15(3 – 1) + (7 – 2)]
= 2215 + 2[15 × 2 + 5]
= 2215 + 2[30 + 5]
= 2215 + 2 × 35
= 2215 + 70
= 2285

Year 2021 : A matrix M[-2…7, 2…10] is stored in the memory with each
element requiring 2 bytes of storage. If the base address is 2140, find the
address of M[5][6] when the matrix is stored in row major wise.
B = 2140, Lr = -2, Lc = 2, C = 10 – 2 + 1 = 9, I = 5, J = 6, W = 2
Address of M[5][6] = B + W[C(I – Lr) + (J – Lc)]
= 2140 + 2[9(5 – (-2)) + (6 – 2)]
= 2140 + 2[9 × 7 + 4]
= 2140 + 2[63 + 4]
= 2140 + 2 × 67
= 2140 + 134
= 2274

Year 2020 : A matrix B[10][20] is stored in the memory with each element
requiring 2 bytes of storage. If the base address at B[2][1] is 2140, find the
address of B[5][4] when the matrix is stored in column major wise.
B = 2140, Lr = 2, Lc = 1, R = 10, I = 5, J = 4, W = 2
Address of B[5][4] = B + W[R(J – Lc) + (I – Lr)]
= 2140 + 2[10(4 – 1) + (5 – 2)]
= 2140 + 2[10 × 3 + 3]
= 2140 + 2[30 + 3]
= 2140 + 2 × 33
= 2140 + 66
= 2206

Year 2019 : A matrix ARR[-4…6, 3…8] is stored in the memory with each
element requiring 4 bytes of storage. If the base address is 1430, find the
address of ARR[3][6] when the matrix is stored in row major wise.
B = 1430, Lr = -4, Lc = 3, C = 8 – 3 + 1 = 6, I = 3, J = 6, W = 4
Address of ARR[3][6] = B + W[C(I – Lr) + (J – Lc)]
= 1430 + 4[6(3 – (-4)) + (6 – 3)]
= 1430 + 4[6(3 + 4) + 3]
= 1430 + 4[6 × 7 + 3]
= 1430 + 4[42 + 3]
= 1430 + 4 × 45
= 1430 + 180
= 1610

Year 2018 : A matrix A[m][m] is stored in the memory with each element
requiring 4 bytes of storage. If the base address at A[1][1] is 1500 and the
address of A[4][5] is 1608, determine the order of the matrix when it is stored in
column major wise.
B = 1500, Lr = 1, Lc = 1, R = m, I = 4, J = 5, W = 4, Address of A[4][5] = 1608

⇒ 1608 = 1500 + 4[m(5 – 1) + (4 – 1)]


Address of A[4][5] = B + W[R(J – Lc) + (I – Lr)]

⇒ 1608 = 1500 + 4[4m + 3]


⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 96 = 16m
⇒6=m

Year 2017 : A matrix P[15][10] is stored with each element requiring 8 bytes of
storage. If the base address at P[0][0] is 1400, determine the address at P[10]
[7] when the matrix is stored in row major wise.
B = 1400, Lr = 0, Lc = 0, C = 10, I = 10, J = 7, W = 8
Address of P[15][10] = B + W[C(I – Lr) + (J – Lc)]
= 1400 + 8[10(10 – 0) + (7 – 0)]
= 1400 + 8[10 × 10 + 7]
= 1400 + 8[100 + 7]
= 1400 + 8 × 107
= 1400 + 856
= 2256

Year 2016 : A matrix A[m][n] is stored with each element requiring 4 bytes of
storage. If the base address at A[1][1] is 1500 and the address at A[4][5] is
1608, determine the number of rows of the matrix when the matrix is stored in
column major wise.
B = 1500, Lr = 1, Lc = 1, R = m, I = 4, J = 5, W = 4, Address of A[4][5] = 1608

⇒ 1608 = 1500 + 4[m(5 – 1) + (4 – 1)]


Address of A[4][5] = B + W[R(J – Lc) + (I – Lr)]

⇒ 1608 = 1500 + 4[4m + 3]


⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 96 = 16m
⇒6=m

Year 2015 : The array D[-2…10][3…8] contains double type elements. If the
base address is 4110, find the address of D[4][5], when the array is stored in
column major wise.
B = 4110, Lr = -2, Lc = 3, R = 10 – (-2) + 1 = 13, I = 4, J = 5, W = 8
Address of D[4][5] = B + W[R(J – Lc) + (I – Lr)]
= 4110 + 8[13(5 – 3) + (4 – (-2))]
= 4110 + 8[26 + 6]
= 4110 + 8 × 32
= 4110 + 256
= 4366

Year 2014 : An array AR[-4…6, -2…12], stores elements in row major wise, with
the address AR[2][3] as 4142. If each element requires 2 bytes of storage, find
the base address.
Address at AR[2][3] = 4142, Lr = -4, Lc = -2, C = 12 – (-2) + 1 = 15, I = 2, J = 3,
W=2

⇒ 4142 = B + 2[15(2 – (-4)) + (3 – (-2))]


Address of AR[2][3] = B + W[C(I – Lr) + (J – Lc)]

⇒ 4142 = B + 2[15(2 + 4) + (3 + 2)]


⇒ 4142 = B + 2[90 + 5]
⇒ 4142 = B + 2 × 95
⇒ 4142 = B + 190
⇒ B = 3952

Year 2013 : 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.
B = 1840, Lr = 0, Lc = 0, C = 10, I = 4, J = 8, W = 4
Address of M[4][8] = B + W[C(I – Lr) + (J – Lc)]
= 1840 + 4[10(4 – 0) + (8 – 0)]
= 1840 + 4[10 × 4 + 8]
= 1840 + 4 × 48
= 1840 + 192
= 2032

Year 2012 : A matrix B[10][7] is stored in the memory with each element
requiring 2 bytes of storage. If the base address at B[x][1] is 1012 and the
address at B[7][3] is 1060, determine the value ‘x’ where the matrix is stored in
column major wise.
Address at B[7][3] = 1060, B = 1012, Lr = x, Lc = 1, R = 10, I = 7, J = 3, W = 2

⇒ 1060 = 1012 + 2[10(3 – 1) + (7 – x)]


Address at B[7][3] = B + W[R(J – Lc) + (I – Lr)]

⇒ 1060 = 1012 + 2[20 + 7 – x]


⇒ 1060 = 1012 + 2[27 – x]
⇒ 1060 = 1012 + 54 – 2x
⇒ 1060 = 1066 – 2x
⇒ -6 = -2x
⇒3=x

Year 2011 : A square matrix A[m × m] is stored in the memory with each
element requiring 2 bytes of storage. If the base address A[1][1] is 1098 and the
address at A[4][5] is 1144, determine the order of the matrix A[m × m] when
the matrix is stored in Column Major Wise.
⇒ 1144 = 1098 + 2[m(5 – 1) + ( 4 – 1)]
Address of A[4][5] = B + W[r(J – Lc) + (I – Lr)]

⇒ 1144 = 1098 + 2[4m + 3]


⇒ 1144 = 1098 + 8m + 6
⇒ 1144 = 1104 + 8m
⇒ 40 = 8m
⇒ m = 5.
So, the order of the matrix is 5 × 5.

Year 2010 : 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 in Column Major Wise.
Each character requires two bytes of storage.

Address of B[7][6] = B + W[r(J – Lc) + (I – Lr)]


= 1046 + 2[7(3 – 0) + (2 – 0)]
= 1046 + 2[21 + 2]
= 1046 + 46
= 1092.

Address Calculation in single (one) Dimension


Array
Array 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 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 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 A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]

Address Calculation in Double (Two)


Dimensional Array
While storing the elements of a 2-D array in memory, these are allocated contiguous memory
locations. Therefore, a 2-D array must be linearized so as to enable their storage. There are two
alternatives to achieve linearization: Row-Major and Column-Major.

Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given:
(1) Row Major System (2) Column Major System
Row Major System:
The address of a location in Row Major System is calculated using the following formula:
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]

Column Major System:


The address of a location in Column Major System is calculated using the following formula:
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 is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix

Important : Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] ) but
if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and columns are calculated
using the following methods:

Number of rows (M) will be calculated as = (Ur – Lr) + 1


Number of columns (N) will be calculated as = (Uc – Lc) + 1

And rest of the process will remain same as per requirement (Row Major Wise or Column Major Wise).

Examples:
Q 1.
An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is 1500
determine the location of X [15][20].

Solution:
As you see here the number of rows and columns are not given in the question. So they are calculated
as:
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26
Number or rows say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

(i) Column Major Wise Calculation of above equation


The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26
Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)]
= 1500 + 1 * [30 + 26 * 5]
= 1500 + 1 * [160]
= 1660 [Ans]

(ii) Row Major Wise Calculation of above equation


The given values are: 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* [26 * (15 – (-15))) + (20 – 15)]
= 1500 + 1 * [26 * 30 + 5]
= 1500 + 1 * [780 + 5]
= 1500 + 785
= 2285 [Ans]

You might also like