ProgrammingVery hard
int a[4][4] = {0}; for (int i = 0; i < 4; i++) a[i][i] = a[i][3 - i] = 1; int c = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) c += a[i][j]; printf("%d", c); What is printed?
- A. 4
- B. 6
- C. 8
- D. 2
Correct answer: C. 8
Each row sets two cells, (i, i) and (i, 3 - i). For a 4 x 4 matrix these are always different cells, so 4 rows x 2 cells = 8 ones.
More C Programming questions
- A 5 x 5 grid is stored as an int matrix. Which expression gives the number of cells on its main diagonal?
- int n = 4; int m[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][j] = i + j; printf("%d", m[3][3]); What is printed?
- Which statement about passing a 2D array to a function is correct in C99?
- int a[3][3]; printf("%zu", sizeof a[0][0] 3 3); What is printed with 4-byte int?
- A 2D grid of 1000 x 1000 is stored as int g[1000][1000] global. Roughly how many bytes does it take with 4-byte int?
- int d[2][3] = {1, 2, 3, 4, 5, 6}; printf("%d", d[1][0]); What is printed?