Dark Mode

1,977 views

2 votes

What is printed by the following $\text{ANSI C}$ 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; }

- $\begin {array}{} 1 & 2 & 3 \\ 10 & 11 & 12 \\ 19 & 20 & 21 \end{array}$
- $\begin {array}{} 1 & 4 & 7 \\ 10 & 13 & 16 \\ 19 & 22 & 25 \end{array}$
- $\begin {array}{} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}$
- $\begin {array}{} 1 & 2 & 3 \\ 13 & 14 & 15 \\ 25 & 26 & 27 \end{array}$

5 votes

Best answer

Option A is correct

a is 3D array with size [3][3][3]

Therefore each 2D array contains 9 elements, we have 3 such arrays

0th 2D array have {1,2,3,4,5,6,7,8,9}

1st 2D array have {10,11,12,13,14,15,16,17,18}

2nd 2D array have {19,20,21,22,23,24,25,26,27}

each 2D array is collection of 1D array. we have 3 one dimensional arrays in one 2D

$\therefore \{\;\;{\underset{k=0,1,2\;\;}{\underbrace{1,2,3}}},\underset{k=0,1,2\;\;}{\underbrace{4,5,6}},\underset{k=0,1,2\;\;}{\underbrace{7,8,9}}\;\;\},$

$ \{{\underset{k=0,1,2\;\;}{\underbrace{10,11,12}}},\underset{k=0,1,2\;\;}{\underbrace{13,14,15}},\underset{k=0,1,2\;\;}{\underbrace{16,17,18}\}},$

$\{{\underset{k=0,1,2\;\;}{\underbrace{19,20,21}}},\underset{k=0,1,2\;\;}{\underbrace{22,23,24}},\underset{k=0,1,2\;\;}{\underbrace{25,26,27}\}}$

when i=0, j=0, output = {1,2,3}

when i=1, j=0, output = {10,11,12}

when i=2, j=0, output = {19,20,21}