ProgrammingModerate
In an n x n row-major int matrix with n = 5, what is the flat index of the diagonal cell (i, i) for i = 2?
- A. 12
- B. 10
- C. 6
- D. 4
Correct answer: A. 12
Row-major index = row * n + col = 2 * 5 + 2 = 12; along the diagonal each step adds n + 1 = 6. 10 forgets the column offset, 6 uses 1-based indices (i - 1) * n + (i - 1), and 4 drops the row stride (i + i).
More C Programming questions
- On a 5 x 5 grid the number of cells on its boundary (first and last row, first and last column) is what?
- Which 2 x 2 product gives the top-left entry of A = [[1, 2], [3, 4]] times B = [[5, 6], [7, 8]]?
- A 3 x 3 grid has one blocked cell at row 1, column 1. How many right-or-down paths lead from the top-left to the bottom-right cell?
- The zigzag scan of the 3 x 3 matrix [[1,2,3],[4,5,6],[7,8,9]] along anti-diagonals, alternating direction (as in JPEG order), gives which…
- In the 3 x 3 matrix [[1,2,3],[4,5,6],[7,8,9]], what is the sum of the boundary cells, leaving the centre out?
- For a 4 x 4 matrix with a[i][j] = 4i + j, what is the sum of the anti-diagonal cells (i, 3 - i)?