What is printed by the following program?
#include<stdio.h>
int main (int argc, char *argv[])
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for ( i = 0; i < 3; i ++) {
for ( k = 0; k < 3; k++)
printf(“%d”, a[i][j][k]);
printf (“\n”);
}
return 0;
}
View worked solution
Answer: A
The 3D array a[3][3][3] consists of two-dimensional arrays, each of size . The initialization assigns values row-by-row to these sub-arrays:
- For :
a[0]is initialized with values to . - For :
a[1]is initialized with values to . - For :
a[2]is initialized with values to .
In the nested loop, the variable j remains constant at . The loop prints a[i][0][k] for and :
- When : Prints
a[0][0][0],a[0][0][1],a[0][0][2]1 2 3 - When : Prints
a[1][0][0],a[1][0][1],a[1][0][2]10 11 12 - When : Prints
a[2][0][0],a[2][0][1],a[2][0][2]19 20 21
Thus, the correct option is A.
Free practice · Your answers on this page are not saved.
