Skip to study material
← Question index

QUESTION 38 · Matrices and Dimensions

Explain the declaration and access of multidimensional arrays.

5 marks

Answer in brief

A multidimensional array has more than one level of array nesting. A three-dimensional array can represent groups of row-and-column tables.

Declaration

int a[2][3][4] = {0};
a[1][2][3] = 7;

Meaning of dimensions

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.

Storage and access

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.

Traversal

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.

Complexity and uses

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.

C program and output

/* 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;
}

Sample input and output

This example uses values already written in the program.

Output

Selected = 7
Elements = 24

Related exam questions

Related questions in the multidimensional arrays category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • July 2021 · Q3(a) · 7 marks

    Using a two-dimensional array, write a C program to transpose a 2 × 2 matrix.

    Paper P04 · page 1
  • September 2023 · Q1(d) · 1 marks

    What is a two-dimensional array?

    Paper P06 · page 1

View this category in the exam question bank →