QUESTION 37 · Matrices and Dimensions
How many int elements are in int a[2][3][4]?
2 marksAnswer
There are 2 × 3 × 4 = 24 int elements.
- Use three valid indices to select one int element.
- The last index changes fastest in the storage order.
QUESTION 37 · Matrices and Dimensions
There are 2 × 3 × 4 = 24 int elements.
QUESTION 38 · Matrices and Dimensions
A multidimensional array has more than one level of array nesting. A three-dimensional array can represent groups of row-and-column tables.
int a[2][3][4] = {0};
a[1][2][3] = 7;The declaration creates two elements at the first level. Each is an array of three row arrays, and each row holds four integers. The valid index ranges are 0..1, 0..2 and 0..3. The assignment changes one selected int to 7. The {0} initialiser gives all elements zero before that assignment.
C arrays are nested contiguous arrays. The last subscript changes fastest through the element storage order. For dimensions x, y and z, the total element count is x × y × z, and storage is that product times sizeof(element). Every individual index must remain within its corresponding bound.
Three nested loops can visit a three-dimensional array. The outer loop selects a group, the next selects a row and the inner selects an element. A direct selected access still has constant work in the usual model; visiting every position has work proportional to the total element count.
A full traversal takes O(xyz) time. If all dimensions equal n, it takes O(n³). Loop counters use O(1) auxiliary space when the number of dimensions is fixed. Such arrays can model several matrices, time-based tables or small grids. Choose dimensions carefully: increasing each dimension multiplies storage rather than merely adding a few elements.
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
int a[2][3][4] = {0};
a[1][2][3] = 7;
printf("Selected = %d\n", a[1][2][3]);
printf("Elements = %zu\n", sizeof a / sizeof a[0][0][0]);
return 0;
}This example uses values already written in the program.
Selected = 7
Elements = 24Related questions in the multidimensional arrays category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.
Using a two-dimensional array, write a C program to transpose a 2 × 2 matrix.
Paper P04 · page 1What is a two-dimensional array?
Paper P06 · page 1