Consider an array A[2 : 8, −4 : 1, 6 : 10]. Base address of array A is = 400…
2021
Consider an array A[2 : 8, −4 : 1, 6 : 10]. Base address of array A is = 400 and size of each element = 2 word per memory cell. What is the address of A[5, −1, 8] in row major form?
- A.
628
- B.
614
- C.
630
- D.
626
Attempted by 268 students.
Show answer & explanation
Correct answer: B
To find the address of A[5, −1, 8] in row-major order, follow these steps:
Step 1: Determine the number of elements in each dimension:
First dimension (2 to 8): 8 − 2 + 1 = 7 elements.
Second dimension (−4 to 1): 1 − (−4) + 1 = 6 elements.
Third dimension (6 to 10): 10 − 6 + 1 = 5 elements.
Step 2: Use the row-major formula for 3D array:
Address = Base Address + [ (i − lower1) × (size2 × size3) + (j − lower2) × size3 + (k − lower3) ] × element_size
Where:
i = 5, j = −1, k = 8
lower1 = 2, lower2 = −4, lower3 = 6
size2 = 6, size3 = 5, element_size = 2
Step 3: Compute the offset:
(5 − 2) × (6 × 5) = 3 × 30 = 90
(−1 − (−4)) × 5 = 3 × 5 = 15
(8 − 6) = 2
Total offset = 90 + 15 + 2 = 107
Step 4: Compute final address:
Address = 400 + (107 × 2) = 400 + 214 = 614