Consider a two dimensional character array A [1:5, 2:7]. Assuming row major…
2013
Consider a two dimensional character array A [1:5, 2:7]. Assuming row major order of storage determine the address of element A[2][3] if base address is 500.
- A.
501
- B.
506
- C.
507
- D.
508
Attempted by 57 students.
Show answer & explanation
Correct answer: C
Concept
For a 2-D array stored in row-major order, an element's address is the base address plus the number of elements that come before it, times the element size. Counting an element's row offset from the array's lower bounds gives the general formula:
Address(A[i][j]) = Base + [ (i − Lr) × Nc + (j − Lc) ] × S
where Lr, Lc are the lower bounds of the row and column ranges, Nc is the number of columns, and S is the size of one element.
Application
Read the bounds from A[1:5, 2:7]: rows run 1–5 so Lr = 1; columns run 2–7 so Lc = 2.
Columns per row: Nc = 7 − 2 + 1 = 6.
Element size: it is a character array, so S = 1 byte.
Row offset for i = 2: (2 − 1) = 1 full row of 6 elements ahead → 1 × 6 = 6.
Column offset for j = 3: (3 − 2) = 1 element into that row.
Total elements before A[2][3]: 6 + 1 = 7; address = 500 + 7 × 1 = 507.
Cross-check
A[1][2] is the very first element, so it must sit at the base address 500 — substituting i = 1, j = 2 gives 500 + [0×6 + 0] = 500, confirming the bounds and formula are applied correctly. Walking forward one row and one column then lands on 507.