638 views
An array VAL[1..15][1..10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of array VAL is 1500, determine the location of VAL when the array VAL is stored in Row wise. ?

Array VAL is stored row wise starting from location 1500. Each element requires 4B.

VAL is stored at 1500

First, we'll store 1st row 1st column, then 1st row 2nd column, and so on until all elements of 1st row are stored. Then similarly, we store 2nd row, 3rd row and so on -

VAL is stored at 1500 + (0 * 10 + 1) * 4 = 1504

VAL is stored at 1500 + (0 * 10 + 8) * 4 = 1532

VAL is stored at 1500 + (1 * 10 + 0) * 4 = 1540

VAL is stored at 1500 + (11 * 10 + 0) * 4 = 1940

VAL is stored at 1500 + (11 * 10 + 8) * 4 = 1972

Answer - Location of VAL is 1972.

stored in row major order and each element is of 4 bytes

hence, w = 4 bytes

location of any array element in row major order is given by formula

loc(A[i][j]) = Base Address + w * (relative index of element)

i.e.,

loc(A[i][j]) = Base Address + w * [(i-LBi)*n +(j-LBj)]

here,

‘n’ is the number of columns = UBj – LBj +1 = 10 – 1 + 1 = 10

loc(VAL) = 1500 + 4*[(12-1)*10 + (9-1)]

loc(VAL) = 1972
by
2D Array RMO with Base Address =0

Formula to Calculate it

A [i] [j] = [(i-1)*n+j-1]*size of each element+Base address

Now,

Val  = [(12-1)*10+9-1]*4+1500]

1
1,619 views