Address in a row-major array — Aptitude Question
Address in a row-major array: An array A[10][20] of 4-byte integers is stored in row-major order with base address… Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 90 seconds
Question
An array A[10][20] of 4-byte integers is stored in row-major order with base address 1000, indices starting at 0. What is the address of A[3][5]?
Options
- 1160
- 1200
- 1260
- 1320
Show the answer and the worked solution
Correct answer: C. 1260
Worked solution
- In row-major order the elements before A[3][5] are the three complete rows 0, 1 and 2, plus five elements of row 3.
- That is 3 × 20 + 5 = 65 elements.
- At 4 bytes each the offset is 65 × 4 = 260 bytes.
- The address is 1000 + 260 = 1260.
Answer: 1260.
The idea
The row-major formula is base + w × (i × columns + j). Column-major swaps the roles, using base + w × (j × rows + i), and papers often ask for both from the same array.
Shortcut. Each row is 20 × 4 = 80 bytes: three whole rows are 240 bytes and five elements add 20, so 1000 + 260 = 1260.
Common trap. 1320 is 1000 + 4 × 80, which counts row 3 itself as a whole row and ignores the column offset; only rows 0 to 2 precede it.
All Data Structures & Algorithms questions · placement mock tests