Multiple choice

int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain?

  1. 3

  2. 5

  3. 7

  4. 9

  5. 11

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

The 3D array is stored in row-major order. testarray[3][2][2] means 3 blocks of 2x2 arrays. The elements are stored as: [0][0][0]=1, [0][0][1]=2, [0][1][0]=3, [0][1][1]=4, [1][0][0]=5, [1][0][1]=6, [1][1][0]=7, [1][1][1]=8, [2][0][0]=9, [2][0][1]=10, [2][1][0]=11, [2][1][1]=12. So testarray[2][1][0] = 11.