ProgrammingHard

The function void zero(int n, int m[][n]) assigns zero to all cells. Which loop is correct for n x n data?

  1. A. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][j] = 0;
  2. B. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][n] = 0;
  3. C. for (int i = 0; i < n; i++) m[i] = 0;
  4. D. for (int i = 1; i < n; i++) for (int j = 1; j < n; j++) m[i][j] = 0;

Correct answer: A. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][j] = 0;

Both indices must range over 0 to n - 1. The other loops write to a column past the end, assign a whole row to an int, or go one past the end.

More C Programming questions

Exams that ask this topic