Difficulty: Easy
Correct Answer: The program will display the output 0 1 2 1 2 3 2 3 4.
Explanation:
Introduction / Context:
The code writes and then reads a 3x3 matrix using the same Matrix[j][i]
indexing order inside nested loops. Although C++ stores arrays in row-major order, the explicit indices determine which element is updated. Because both the constructor and the display use the same indexing, values are read exactly as written.
Given Data / Assumptions:
Matrix[j][i] = i + j
for i, j = 0..2
.Matrix[j][i]
in the same i
-outer, j
-inner order.
Concept / Approach:
Compute each entry: For i=0
, the row of outputs is 0,1,2
; for i=1
, outputs are 1,2,3
; for i=2
, outputs are 2,3,4
. Since display mirrors initialization order and indexing, no uninitialized values are printed.
Step-by-Step Solution:
Verification / Alternative check:
Swapping indices in Display
to Matrix[i][j]
would produce a different sequence because the matrix is not symmetric in general for arbitrary formulas.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing row-major storage with the iteration order used for printing; storage order does not affect correctness of this deterministic computation.
Final Answer:
The program will display the output 0 1 2 1 2 3 2 3 4.
Discussion & Comments