0% found this document useful (0 votes)
211 views5 pages

Memory Address Excercises

Uploaded by

Arup Sarkar
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)
211 views5 pages

Memory Address Excercises

Uploaded by

Arup Sarkar
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/ 5

Calculate Memory Addresses in Arrays

Posted on September 5, 2019


The 2-dimensional arrays are stored as 1-dimensional arrays in the computer’s memory.

There are two ways to achieve this:

Row-major Implementation

In this method, the first row elements are placed first, then the second row elements and so on.

The formula to calculate the address of [I, J]th block is:

Address of [I, J] element in row-major = B + W[C(I – L ) + (J – L )]


th
r c

Column-major Implementation

In this method, the first column elements are placed first, then the second column elements and so on.

The formula to calculate the address of [I, J]th block is:

Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]


th
c r OR
B + W[ (I – L )+ R(J – L ) ]
r c

Note that:
B is the base address (address of the first block in the array).
W is the width in bytes (size in bytes for each block in the array).
L is the index of the first row.
r

L is the index of the first column.


c

R is the total number of rows.


C is the total number of columns.

Solved Questions based on Array Implementation

ISC 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.

BASE=2140
C=20
R=10
I=5
J=4
Lc=1
Lr=2

Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]


th
c r

= 2140 + 2[10(4 – 1) + (5 – 2)]


= 2140 + 2[10 × 3 + 3]
= 2140 + 2[30 + 3]
= 2140 + 2[33]
= 2140 + 66
= 2206
ISC 2020 Specimen: Each element of an array arr[15][20] requires ‘W’ bytes of storage.
If the address of arr[6][8] is 4440 and the base address at arr[1][1] is 4000, find the width ‘W’
of each cell in the array arr[][] when the array is stored as Column Major Wise.

Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]


th
c r

⇒ 4440 = 4000 + W[15(8 – 1) + (6 – 1)]


⇒ 4440 = 4000 + W[15(7) + 5]
⇒ 4440 = 4000 + W[105 + 5]
⇒ 4440 = 4000 + W[110]
W[110]=4440-4000
W=440/110=4

⇒ W[110] = 440
⇒ W = 4.

ISC 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.

Number of columns, C = 8 – 3 + 1 = 6.
B=1430,W=4,I=3,J=6,Lr=-4,Lc=3
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 – (-4)) + (6 – 3)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 + 4) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[6(7) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[42 + 3]
⇒ Address of ARR[3][6] = 1430 + 4[45]
⇒ Address of ARR[3][6] = 1430 + 180
⇒ Address of ARR[3][6] = 1610.

ISC 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.
Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]
th
c r

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


⇒ 1608 = 1500 + 4[m(4) + 3]
⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 16m = 96
⇒ m = 6.

ISC 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.

BASE=1400
W=8
C=10
R=15
I=10
J=7
Lc=0
Lr=0

B + W[C(I – L ) + (J – L )]
r c

Address of [I, J] element in row-major = B + W[C(I – L ) + (J – L )]


th
r c

⇒ Address at P[10][7] = 1400 + 8[10(10 – 0) + (7 – 0)]


⇒ Address at P[10][7] = 1400 + 8[10(10) + 7]
⇒ Address at P[10][7] = 1400 + 8[100 + 7]
⇒ Address at P[10][7] = 1400 + 8[107]
⇒ Address at P[10][7] = 1400 + 856
⇒ Address at P[10][7] = 2256.

ISC 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.
Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]
th
c r

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


⇒ 1608 = 1500 + 4[4R + 3]
⇒ 1608 = 1500 + 16R + 12
⇒ 1608 = 1512 + 16R
⇒ 16R = 96
⇒ R = 6.

ISC 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.
Number of rows, R = 10 – (-2) + 1 = 13.
Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]
th
c r

⇒ Address of D[4][5] = 4110 + 8[13(5 – 3) + (4 – (-2))]


⇒ Address of D[4][5] = 4110 + 8[13(2) + (4 + 2)]
⇒ Address of D[4][5] = 4110 + 8[26 + 6]
⇒ Address of D[4][5] = 4110 + 8[32]
⇒ Address of D[4][5] = 4110 + 256
⇒ Address of D[4][5] = 4366.

ISC 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.
Number of columns, C = 12 – (-2) + 1 = 12 + 2 + 1 = 15.
Address of [I, J] element in row-major = B + W[C(I – L ) + (J – L )]
th
r c

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


⇒ 4142 = 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
⇒ B = 3952.
ISC 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.
Address of [I, J] element in row-major = B + W[C(I – L ) + (J – L )]
th
r c

⇒ Address at M[4][8] = 1840 + 4[10(4 – 0) + (8 – 0)]


⇒ Address at M[4][8] = 1840 + 4[10(4) + 8]
⇒ Address at M[4][8] = 1840 + 4[40 + 8]
⇒ Address at M[4][8] = 1840 + 4[48]
⇒ Address at M[4][8] = 1840 + 192
⇒ Address at M[4][8] = 2032.
ISC 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 of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]
th
c r

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


⇒ 1060 = 1012 + 2[10(2) + 7 – x]
⇒ 1060 = 1012 + 2[20 + 7 – x]
⇒ 1060 = 1012 + 2[27 – x]
⇒ 1060 = 1012 + 54 – 2x
⇒ 1060 = 1066 – 2x
⇒ -2x = -6
⇒ x = 3.
ISC 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 at 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.
Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]
th
c r

⇒ 1144 = 1098 + 2[m(5 – 1) + (4 – 1)]


⇒ 1144 = 1098 + 2[m(4) + 3]
⇒ 1144 = 1098 + 8m + 6
⇒ 1144 = 1104 + 8m
⇒ 8m = 40
⇒ m = 5.
ISC 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 2 bytes of storage.
Address of [I, J] element in column-major = B + W[R(J – L ) + (I – L )]
th
c r

⇒ Address at B[2][3] = 1046 + 2[7(3 – 0) + (2 – 0)]


⇒ Address at B[2][3] = 1046 + 2[7(3) + 2]
⇒ Address at B[2][3] = 1046 + 2[21 + 2]
⇒ Address at B[2][3] = 1046 + 2[23]
⇒ Address at B[2][3] = 1046 + 46
⇒ Address at B[2][3] = 1092.
ISC Year 2009: 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 in Row Major Wise.
Address of [I, J] element in row-major = B + W[C(I – L ) + (J – L )]
th
r c

⇒ 4000 = 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
⇒ B = 3864.
ISC Year 2008: A two-dimensional array defined as X[3…6, -2…2] requires 2 bytes of storage space for each element. If
the array is stored in Row Major Wise order, determine the address of X[5][1], given the base address as 1200.
Number of columns, C = 2 – (-2) + 1 = 5.
Address of [I, J] element in row-major = B + W[C(I – L ) + (J – L )]
th
r c

⇒ Address of X[5][1] = 1200 + 2[5(5 – 3) + (1 – (-2))]


⇒ Address of X[5][1] = 1200 + 2[5(2) + (3)]
⇒ Address of X[5][1] = 1200 + 2[13]
⇒ Address of X[5][1] = 1200 + 26
⇒ Address of X[5][1] = 1226.

You might also like