Lesson 42 of 60 – Multidimensional Arrays
70%

Multidimensional Arrays in C

A multidimensional array in C is an array that contains more than one dimension. A 2D array has rows and columns, while a 3D array can be viewed as multiple 2D arrays or layers. Multidimensional arrays are useful for storing data such as matrices, tables, cubes, marks, and other structured information.

Note: A 3D array requires three indexes to access one element. For example, arr[1][2][3] uses three indexes.

1. What is a Multidimensional Array?

A multidimensional array is an array having two or more dimensions. It allows us to store data in multiple levels.

int numbers[2][3];

The above array is a two-dimensional array with 2 rows and 3 columns.

2. Dimensions of an Array

A dimension represents one level of indexing in an array.

  • 1D array → one index
  • 2D array → two indexes
  • 3D array → three indexes
  • 4D array → four indexes

The number of dimensions determines how many indexes are needed to access an element.

3. 3D Array

A 3D array is an array containing multiple 2D arrays.

int arr[2][3][4];

This array has 2 layers, each layer has 3 rows, and each row has 4 columns.

4. Syntax of a 3D Array

The general syntax for declaring a 3D array is:

data_type array_name[size1][size2][size3];

Example:

int marks[2][3][4];

Here, 2, 3, and 4 represent the three dimensions.

5. Understanding Layers

A 3D array can be imagined as a collection of 2D tables placed one after another.

int data[2][2][3];

This contains 2 layers. Each layer contains 2 rows and each row contains 3 elements.

6. Accessing Elements

Three indexes are used to access an element of a 3D array.

int arr[2][3][4];

arr[0][1][2] = 50;

The indexes represent the layer, row, and column.

7. Indexing Starts from Zero

Array indexes in C start from zero. For an array declared as:

int arr[2][3][4];

The valid indexes are:

  • First dimension: 0 to 1
  • Second dimension: 0 to 2
  • Third dimension: 0 to 3

8. Initializing a 3D Array

A 3D array can be initialized when it is declared.

int arr[2][2][3] = {
    {
        {1, 2, 3},
        {4, 5, 6}
    },
    {
        {7, 8, 9},
        {10, 11, 12}
    }
};

9. Reading a Value

Use three indexes when reading a value from a 3D array.

printf("%d", arr[1][0][2]);

This accesses the element at layer 1, row 0, and column 2.

10. Changing an Element

An individual element can be changed using its indexes.

arr[0][1][2] = 100;

Only that particular element is changed.

11. Nested Loops for 3D Arrays

Three nested loops are commonly used to process all elements of a 3D array.

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 3; j++)
    {
        for(int k = 0; k < 4; k++)
        {
            printf("%d ", arr[i][j][k]);
        }
    }
}

12. Loop Variables

Usually, one loop variable is used for each dimension.

i → layer
j → row
k → column

This makes the nested loops easier to understand.

13. Displaying a 3D Array

The following program displays all values of a 3D array.

#include <stdio.h>

int main()
{
    int arr[2][2][2] = {
        {{1, 2}, {3, 4}},
        {{5, 6}, {7, 8}}
    };

    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            for(int k = 0; k < 2; k++)
            {
                printf("%d ", arr[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

14. Taking Input in a 3D Array

Input can also be taken using three nested loops.

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        for(int k = 0; k < 2; k++)
        {
            scanf("%d", &arr[i][j][k]);
        }
    }
}

15. Finding the Sum

A nested loop can be used to calculate the sum of all elements.

int sum = 0;

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        for(int k = 0; k < 2; k++)
        {
            sum += arr[i][j][k];
        }
    }
}

16. Counting Elements

You can count elements satisfying a condition.

int count = 0;

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        for(int k = 0; k < 2; k++)
        {
            if(arr[i][j][k] > 10)
            {
                count++;
            }
        }
    }
}

17. Searching in a 3D Array

A value can be searched by checking every element.

int found = 0;

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        for(int k = 0; k < 2; k++)
        {
            if(arr[i][j][k] == 7)
            {
                found = 1;
            }
        }
    }
}

18. Finding the Largest Element

The largest element can be found by comparing every value.

int largest = arr[0][0][0];

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        for(int k = 0; k < 2; k++)
        {
            if(arr[i][j][k] > largest)
            {
                largest = arr[i][j][k];
            }
        }
    }
}

19. Finding the Smallest Element

The smallest value can be found in a similar way.

int smallest = arr[0][0][0];

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        for(int k = 0; k < 2; k++)
        {
            if(arr[i][j][k] < smallest)
            {
                smallest = arr[i][j][k];
            }
        }
    }
}

20. Memory Layout

C stores array elements in contiguous memory according to the array's layout. For multidimensional arrays, the rightmost dimension varies fastest when elements are laid out in memory.

int arr[2][2][3];

Understanding this becomes useful when working with pointers and functions.

21. 3D Array for Cube Data

A 3D array can represent data arranged in layers, rows, and columns. For example, a small cube of values can be represented as:

int cube[3][3][3];

Here there are 3 layers, 3 rows per layer, and 3 columns per row.

22. Passing a 3D Array to a Function

A multidimensional array can be passed to a function. The trailing dimensions generally need to be specified in the parameter type.

void display(int arr[][2][3])
{
    // process array
}

The first dimension may be omitted in the parameter declaration.

23. Function Example

Here is a simple function that displays a 3D array.

void display(int arr[][2][2])
{
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            for(int k = 0; k < 2; k++)
            {
                printf("%d ", arr[i][j][k]);
            }
        }
    }
}

24. 3D Array and sizeof

The sizeof operator can be used to determine the total size of an array when the array itself is in scope.

int arr[2][3][4];

printf("%zu", sizeof(arr));

The result is the total number of bytes occupied by the complete array.

25. Number of Elements

For a fully declared array, the total number of elements can be calculated by dividing its total size by the size of one element.

int arr[2][3][4];

size_t total = sizeof(arr) / sizeof(arr[0][0][0]);

The result is 24 elements.

26. Common Mistake: Wrong Index

Always use indexes within the declared limits.

int arr[2][3][4];

The expression arr[2][0][0] is invalid because the first dimension has valid indexes only from 0 to 1.

Accessing outside the bounds of an array causes undefined behavior.

27. Common Mistake: Incorrect Loop Limits

Loop limits must match the array dimensions.

int arr[2][3][4];

for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 3; j++)
    {
        for(int k = 0; k < 4; k++)
        {
            printf("%d ", arr[i][j][k]);
        }
    }
}

Using larger limits can access elements outside the array.

28. Complete 3D Array Program

#include <stdio.h>

int main()
{
    int arr[2][2][2];
    int sum = 0;

    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            for(int k = 0; k < 2; k++)
            {
                printf("Enter value: ");
                scanf("%d", &arr[i][j][k]);
                sum += arr[i][j][k];
            }
        }
    }

    printf("Sum = %d", sum);

    return 0;
}

This program takes input for every element of a 3D array and calculates the total.

29. Practical Uses of Multidimensional Arrays

Multidimensional arrays can be used in many programming problems.

  • Matrix and mathematical calculations
  • Tables and structured data
  • 3D game or graphics data
  • Scientific data
  • Image and pixel-related data
  • Layer-based information
  • Storage of multiple sets of records

30. Practice Program

Create a C program that:

  1. Creates a 3D integer array.
  2. Takes input from the user.
  3. Displays all elements.
  4. Calculates the sum.
  5. Finds the largest element.
  6. Finds the smallest element.
  7. Counts how many values are even.

Use three nested loops to process all elements.

📌 Key Points

  • A multidimensional array has two or more dimensions.
  • A 3D array requires three indexes to access an element.
  • Nested loops are commonly used to process multidimensional arrays.
  • Array indexes start from 0.
  • Loop limits must match the array dimensions.
  • Accessing outside array bounds causes undefined behavior.
  • 3D arrays can represent layers, rows, and columns.
  • Multidimensional arrays can be passed to functions.
  • The trailing dimensions must be known in a function parameter type.
  • sizeof can be used to calculate the total size of an array when it is in scope.

🧠 Quick Quiz

Question: How many indexes are required to access an element of a 3D array?