C Programming Tutorial
About Lesson

C Multi-dimensional Array

In this tutorial, we will learn about multidimensional arrays (two-dimensional and three-dimensional arrays) and also learn to declare, initialize and access elements with the help of examples.


Multi-dimensional arrays

  • Multidimensional array are also known as array of arrays.
  • The data in multi-dimensional array is stored in a tabular form (row * column) as shown in the diagram below.

General form of declaration N-dimensional arrays :

data_type  array_name[size1][size2]....[sizeN];

data_type: Type of data to be stored in the array. 
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions

Types of multidimensional array :

  1. 2D (Two dimensional) array
  2. 3D (Three dimensional) array

2D Arrays | Two Dimensional Array

  • Two dimensional array is the simplest form of a Multidimensional array.
  • It can be visualized as an array of arrays.
  • 2D array is also called a matrix, we are going to discuss how we can initialize 2D array.
  • It can be of any type like integer, character, float, etc. depending on the Initialization.

Syntax:

Data_type array_name[x][y];

We have to write the 2D array by using two square brackets in which x is used to define the number of rows in array and y is used to define the number of columns of array.

2D array Using Diagram :

C two dimensional array
Elements in two-dimensional array in C Programming

Initialization of two-dimensional array

Like a normal array, we can initialize a multidimensional array in more than one way.

First method :

int arr[2][3] = {2, 0, 3, 5, 1, 12};

The above method is not preferred. A better way to initialize this array with the same array elements is given below:

Better method :

int  arr[2][3] = { {2, 0, 3}, {5, 1, 12}};

So, as you can see, we initialize a 2D array arr, with 2 rows and 3 columns as an array of arrays.

Total elements in this array : 2 * 3 = 6 , and Each element of the array is yet again an array of integers.

         Col 1    Col 2    Col 3
Row 1       2       0       3
Row 2       5       1       12

Example : Taking Input from user for Two Dimensional Array

// C program to take input from user and print output
#include <stdio.h>
int main(){
  int a[2][2], i, j;
  // Taking input using nested for loop
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      printf("Enter a[%d][%d] : ", i + 1, j + 1);
      scanf("%d", &a[i][j]);
    }
  }
  printf("\n All the elements of matrix : \n");
  // Printing Output using nested for loop
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < 2; ++j) {
      printf("a[%d][%d] : %d\n", i + 1, j + 1, a[i][j]);
    }
  }
  return 0;
}

Output :

Enter a[1][1] : 1
Enter a[1][2] : 2
Enter a[2][1] : 3
Enter a[2][2] : 4
All the elements of matrix :
a[1][1] : 1
a[1][2] : 2
a[2][1] : 3
a[2][2] : 4

Working:

Here, we have used a nested for loop to take the input of the 2d array. Once all the input has been taken, we have used another nested for loop to print the array members.

Finally, we print the array elements in each iteration.


3D Array | Three dimensional Array

  • Three dimensional array is form of a Multidimensional array.
  • 3D array is a multi-dimensional array used to store 3-dimensional information.
  • 3D array is essentially an array of arrays of arrays : it’s an array or collection od 2D array. and a 2D array is an array of 1D array.
  • It can be of any type like integer, character, float, etc. depending on the Initialization.

Syntax :

Data_type array_name[x][y][z];

We have to write the 3D array by using three square brackets in which x is used to define the number of rows in array, y is used to define the number of columns of array, and z is used to define the number of blocks of array.

Initialization of three-dimensional array

Initialization in 3D array is same as that of 2D array.

First method :

int arr[2][3][4] = {2, 1, 0, 2, 3, -1, 5, 10, 13, 7, 20, 
    12, 3, -4, 6, -2, 8, -1, 5, -5, -2, 11, 14, 0};

The above method is not a good way of initializing a three-dimensional array.

Better method :

int arr[2][3][4] = { 
    { {2, 1, 0, 2}, {3, -1, 5, 10}, {13, 7, 20, 12} },
    { {3, -4, 6, -2}, {8, -1, 5, -5}, {-2, 11, 14, 0} }
};

Notice the dimensions of this three-dimensional array.

The first dimension has the value 2. So, the two elements comprising the first dimension are:

Element 1 = { {2, 1, 0, 2}, {3, -1, 5, 10}, {13, 7, 20, 12} }
Element 2 = { {3, -4, 6, -2}, {8, -1, 5, -5}, {-2, 11, 14, 0} }

The second dimension has the value 3. Notice that each of the elements of the first dimension has three elements each:

{2, 1, 0, 2}, {3, -1, 5, 10} and {13, 7, 20, 12} for Element 1.
{3, -4, 6, -2}, {8, -1, 5, -5} and {-2, 11, 14, 0} for Element 2.

Finally, there are four int numbers inside each of the elements of the second dimension:

{2, 1, 0, 2}
{3, -1, 5, 10}
{13, 7, 20, 12}
... .. ...
... .. ...

Example : Taking Input from user for 3D Array

// C program to take input from user and print output
#include <stdio.h>
int main(){
  int a[2][3][4], i, j, k;
  // Taking input using nested for loop
  for (int i = 0; i < 2; i++) {
  for (int j = 0; j < 3; j++) {
    for (int k = 0; k < 4; k++) {
      printf("Enter a[%d][%d][%d] : ", i + 1, j + 1, k + 1);
      scanf("%d", &a[i][j][k]);
    }
  }
  printf("\n All the elements of matrix : \n");
  // Printing Output using nested for loop
  for (i = 0; i < 2; ++i) {
  for (j = 0; j < 3; ++j) {
    for (k = 0; k < 4; ++k) {
      printf("a[%d][%d][%d] : %d\n", i + 1, j + 1, i + 1, a[i][j][k]);
    }
  }
  return 0;
}

Output :

Enter a[1][1][1] : 1
Enter a[1][1][2] : 2
Enter a[1][1][3] : 3
Enter a[1][1][4] : 4
Enter a[1][2][1] : 5
Enter a[1][2][2] : 6
Enter a[1][2][3] : 7
Enter a[1][2][4] : 8
Enter a[1][3][1] : 9
Enter a[1][3][2] : 10
Enter a[1][3][3] : 11
Enter a[1][3][4] : 12
Enter a[2][1][1] : 13
Enter a[2][1][2] : 14
Enter a[2][1][3] : 15
Enter a[2][1][4] : 16
Enter a[2][2][1] : 17
Enter a[2][2][2] : 18
Enter a[2][2][3] : 19
Enter a[2][2][4] : 20
Enter a[2][3][1] : 21
Enter a[2][3][2] : 22
Enter a[2][3][3] : 23
Enter a[2][3][4] : 24
All the elements of matrix :
a[1][1][1] : 1
a[1][1][2] : 2
a[1][1][3] : 3
a[1][1][4] : 4
a[1][2][1] : 5
a[1][2][2] : 6
a[1][2][3] : 7
a[1][2][4] : 8
a[1][3][1] : 9
a[1][3][2] : 10
a[1][3][3] : 11
a[1][3][4] : 12
a[2][1][1] : 13
a[2][1][2] : 14
a[2][1][3] : 15
a[2][1][4] : 16
a[2][2][1] : 17
a[2][2][2] : 18
a[2][2][3] : 19
a[2][2][4] : 20
a[2][3][1] : 21
a[2][3][2] : 22
a[2][3][3] : 23
a[2][3][4] : 24

The basic concept of printing elements of a 3d array is similar to that of a 2d array.

However, since we are manipulating 3 dimensions, we use a 3 nested for loop :

  • the outer loop from i == 0 to i == 1 accesses the first dimension of the array
  • the middle loop from j == 0 to j == 2 accesses the second dimension of the array
  • the inner loop from k == 0 to k == 1 accesses the third dimension of the array

Finally, we print the array elements in each iteration.

  • In similar ways, we can create arrays with any number of dimension. However, the complexity also increases as the number of dimensions increases.
  • Note : The most used multidimensional array is the 2D array.
error: Content is protected !!