ITPEC FE Morning October 2022 Question 8
When a two-dimensional array A(5,5) is mapped to computer memory (i.e. a one-dimensional array) in row-major (row-directional) order or column-major (column-directional) order, how many elements occupy the same memory addresses in both cases? Here, the first element A(1,1) is mapped to the same starting memory address in either case.
Explanation
5 — an element has the same address in both layouts exactly when its row and column numbers are equal, the main diagonal.
For A(i, j) with 1 ≤ i, j ≤ 5, counting positions from 0:
- •Row-major position: 5(i − 1) + (j − 1)
- •Column-major position: 5(j − 1) + (i − 1)
- •Equal when 4(i − 1) = 4(j − 1), that is i = j
So A(1, 1), A(2, 2), A(3, 3), A(4, 4) and A(5, 5) share their address, 5 elements.
Answer (d)
Why not others:
- •(a) 2 — counts only the first and last elements, A(1, 1) and A(5, 5), which obviously coincide at both ends
- •(b) 3 and (c) 4 — stop short of the whole diagonal; every A(i, i) sits at position 6(i − 1) in both layouts
Key rule: In an n × n array, row-major and column-major order place only the diagonal elements at the same address, so exactly n elements match.
Algorithm and Programming · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.