Course Content
Basic of C programming
0/14
Functions, Array and Structures
0/11
C Programming Pointers
0/1
C Pointers & Arrays
0/1
C Pointers & Functions
0/1
C Memory Allocation
0/1
C Programming String
0/1
C String Functions
0/1
C Files Input/Output
0/1
C Enumeration
0/1
C Preprocessors
0/1
C Standard Library
0/1
C Programming Tutorial

## 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 :

### 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] : 1Enter a[1][2] : 2Enter a[2][1] : 3Enter a[2][2] : 4All the elements of matrix : a[1][1] : 1a[1][2] : 2a[2][1] : 3a[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] : 1Enter a[1][1][2] : 2Enter a[1][1][3] : 3Enter a[1][1][4] : 4Enter a[1][2][1] : 5Enter a[1][2][2] : 6Enter a[1][2][3] : 7Enter a[1][2][4] : 8Enter a[1][3][1] : 9Enter a[1][3][2] : 10Enter a[1][3][3] : 11Enter a[1][3][4] : 12Enter a[2][1][1] : 13Enter a[2][1][2] : 14Enter a[2][1][3] : 15Enter a[2][1][4] : 16Enter a[2][2][1] : 17Enter a[2][2][2] : 18Enter a[2][2][3] : 19Enter a[2][2][4] : 20Enter a[2][3][1] : 21Enter a[2][3][2] : 22Enter a[2][3][3] : 23Enter a[2][3][4] : 24All the elements of matrix : a[1][1][1] : 1a[1][1][2] : 2a[1][1][3] : 3a[1][1][4] : 4a[1][2][1] : 5a[1][2][2] : 6a[1][2][3] : 7a[1][2][4] : 8a[1][3][1] : 9a[1][3][2] : 10a[1][3][3] : 11a[1][3][4] : 12a[2][1][1] : 13a[2][1][2] : 14a[2][1][3] : 15a[2][1][4] : 16a[2][2][1] : 17a[2][2][2] : 18a[2][2][3] : 19a[2][2][4] : 20a[2][3][1] : 21a[2][3][2] : 22a[2][3][3] : 23a[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 !!