in Programming
638 views
2 votes
2 votes
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[12][9] when the array VAL is stored in Row wise. ?
in Programming
638 views

3 Answers

1 vote
1 vote

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

VAL[1][1] 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[1][2] is stored at 1500 + (0 * 10 + 1) * 4 = 1504

VAL[1][9] is stored at 1500 + (0 * 10 + 8) * 4 = 1532

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

VAL[12][1] is stored at 1500 + (11 * 10 + 0) * 4 = 1940

VAL[12][9] is stored at 1500 + (11 * 10 + 8) * 4 = 1972

Answer - Location of VAL[12][9] is 1972. 

1 vote
1 vote
base address = b =1500

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[12][9]) = 1500 + 4*[(12-1)*10 + (9-1)]

loc(VAL[12][9]) = 1972
0 votes
0 votes
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] [9]= [(12-1)*10+9-1]*4+1500]