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