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?

  1. A. 12
  2. B. 10
  3. C. 6
  4. 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

Exams that ask this topic