What is printed by the following ANSI C program?
#include\(\lt\)stdio.h\(\gt\)
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 \(\lt\) 3; i++ ){
for(k = 0; k \(\lt\) 3; k++ )
printf("%d ", a[i][j][k]);
printf("\n");
}
return 0;
}
GATE CS - 2022,
Answer: A
The program prints 1 2 3 / 10 11 12 / 19 20 21, one group per line.
Each inner brace list initialises one 3 by 3 block a[i] with nine values, filled row by row, so a[0] holds 1 to 9, a[1] holds 10 to 18 and a[2] holds 19 to 27. In particular a[i][0] is the first row of each block: 1 2 3, then 10 11 12, then 19 20 21.
The loops vary i and k but never change j, which stays 0. So for each i the program prints a[i][0][0], a[i][0][1], a[i][0][2] and then a newline, which is the first row of each block.
'1 4 7 ...' would come from varying j instead of k. '1 2 3 / 4 5 6 / 7 8 9' prints the three rows of a[0] only, as if i stayed 0 while j changed. '1 2 3 / 13 14 15 / 25 26 27' moves along the diagonal blocks, as if j rose with i.
Please log in or register to report this question.