In: Computer Science
What's the memory address for the following elements arrays
given:
Base Address: 1000, Dimensions: 10 rows, 5 columns, 4 byte
elements.
Elements: 0,0; 9,4; 2,2; 4,1;
Base Address: 500, Dimensions: 10 rows, 15 columns, 4 byte
elements.
Elements: 0,0; 9,14; 2,2; 4,11;
Base Address: 800, Dimensions: 4 rows, 5 columns, 2 byte
elements.
Elements: 0,0; 3,4; 2,2; 3,4;
Solution:
(a)
Given,
=>Base address = 1000
=>Number of rows = 10
=>Number of columns = 5
=>Size of each element = 4 bytes
(i)
Explanation:
=>By default row-major order is assumed.
=>In row-major order first one full row is traversed before traversing another row,
Calculating address of 0,0:
=>Address of element = base address + size of element(i*number of rows + j*number of columns)
Where i = first index of array and j = second index of array
=>Address of element = 1000 + 4*(0*10 + 0*5)
=>Address of element = 1000
(ii)
Explanation:
Calcualting address of 9,4:
=>Address of element = 1000 + 4*(9*10 + 4*5)
=>Address of element = 1000 + 4*110
=>Address of element = 1000 + 440
=>Address of element = 1440
(iii)
Explanation:
Calcualting address of 2,2:
=>Address of element = 1000 + 4*(2*10 + 2*5)
=>Address of element = 1000 + 4*30
=>Address of element = 1000 + 120
=>Address of element = 1120
(iv)
Explanation:
Calculating address of 4,1:
=>Address of element = 1000 + 4*(4*10 + 1*5)
=>Address of element = 1000 + 4*45
=>Address of element = 1000 + 180
=>Address of element = 1180
(b)
Given,
=>Base address = 500
=>Number of rows = 10
=>Number of columns = 15
=>Size of each element = 4 bytes
(i)
Explanation:
Calculating address of 0,0:
=>Address of element = 500 + 4*(0*10 + 0*15)
=>Address of element = 500
(ii)
Explanation:
Calculating address of 9,14:
=>Address of element = 500 + 4*(9*10 + 14*15)
=>Address of element = 500 + 4*300
=>Address of element = 500 + 1200
=>Address of element = 1700
(iii)
Explanation:
Calculating address of 2, 2:
=>Address of element = 500 + 4*(2*10 + 2*15)
=>Address of element = 500 + 4*50
=>Address of element = 500 + 200
=>Address of element = 700
(iv)
Explanation:
Calculating address of 4,11:
=>Address of element = 500 + 4*(4*10 + 11*15)
=>Address of element = 500 + 4*205
=>Address of element = 500 + 820
=>Address of element = 1320
(c)
=>Base address = 800
=>Number of rows = 4
=>Number of columns = 5
=>Size of each element = 2 bytes
(i)
Explanation:
Calculating address of 0,0:
=>Address of element = 800 + 2*(0*4 + 0*5)
=>Address of element = 800
(ii)
Explanation:
Calculating address of 3,4:
=>Address of element = 800 + 2*(3*4 + 4*5)
=>Address of element = 800 + 64
=>Address of element = 864
(iii)
Explanation:
Calculating address of 2,2:
=>Address of element = 800 + 2*(2*4 + 2*5)
=>Address of element = 800 + 2*18
=>Address of element = 800 + 36
=>Address of element = 836
(iv)
Explanation:
Calculating address of 3,4:
=>Address of element = 800 + 2*(3*4 + 4*5)
=>Address of element = 800 + 2*32
=>Address of element = 800 + 64
=>Address of element = 864
I have explained each and every part with the help of statements attached to it.