+1 vote
150 views

A $2$-dimentional array is declared as  $A[-8 \text{ to } 12, \ -4 \text{ to } 16]$ , what will be the address of $A[1,3]$ if it is stored in row major order , each element occupies $4$ bytes and starting address of array is $2000$.

1. $2784$
2. $2776$
3. $2794$
4. $2764$

edited | 150 views

Original matrix = A[lb1...ub1 , lb2..ub2]

n = size of column (ub2-lb2+1)

then for row major order,

[(i-lb1)*n + (j-lb2)]* size of each element +Base Address

thus A[1,3] = [(1+8)*21 + (3+4)]*4 +2000 = 784+2000 = 2784
by Active (1.3k points)
selected by
None of these answer is 2832.
by (243 points)

Normally, to access $A[1][3]$ we skip 1 rows and 3 columns. Because we assume the first element to be $A[0][0]$

But here, since the first element is at $A[-8][-4]$, to reach $A[1][3]$ skip 9 rows and 7 columns.

=> Skip 9 rows + 7 elements

=> Skip 9*(column length) elements + 7 elements

=> Skip 9(21) elements + 7 elements

=> Skip 189 elements + 7 elements

=> Skip 196 elements

=> Skip 196 * 4 bytes

=> Skip 784 bytes from the base address 2000.

=> 2784

Option A

by Loyal (7k points)