# C Multi-dimensional Arrays

You must learn the basic concepts of the array in order to understand the multidimensional arrays in detail. Learn about the arrays in C by clicking the link below:

Until now we have just learned about the one-dimensional arrays. In this tutorial, you are going to learn about the multidimensional arrays. C language also supports multidimensional arrays. We can say multidimensional arrays as the array of arrays. The simplest form of multidimensional arrays that you are going to learn is the two-dimensional arrays. Two-dimensional arrays will have two dimensions. Such arrays will have elements in row and column wise. They are also known as matrix. The rows’ and column’s index always begins from 0. In general practice, multidimensional arrays might also store associative array.

## How can we initialize Multidimensional arrays?

We can initialize the multidimensional arrays in programs by simply specifying the datatype of the arrays. These data types will be followed by the array name and the number of dimensions with sizes in it as the parameters. Following is the syntax which we can use for declaring the multidimensional arrays.

**The syntax for multidimensional arrays:**

datatype Arrayname[size][size];

For initializing the variables, we can use the following syntaxes. The following syntaxes are for the two-dimensional arrays and three-dimensional arrays respectively.

**Syntax for Two dimensional arrays:**

We can use either of the syntaxes below for ininitializing the two dimensional arrays.

datatype Arrayname[size][size]={list of values}

For example,

int matrix[2][3]={{1,2,3},{4,5,6}};

int matrix[2][3]={1,2,3,4,5,6};

**Syntax for Three dimensional arrays:**

The three dimensional arryays will deploy the following syntaxes for its initiation.

datatype Arrayname[size][size][size]={list of values}

For example,

int matrix[2][3][4]={{1,2,3},{4,5,6},{7,8,9}};

Now check out the following examples to help you more in getting clearer conepts regarding the multidimensional arrays. We have used the nested loop to handle the two dimesional arrays.

The program below simply illustrates the concept of multidimensional arrays. In the following program, the program will take in two matrices each with two dimensions. The program will add those two matrices and display its sum as a result.

**Code:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include<stdio.h> int main(){ int num1[10][10], num2[10][10], sum[10][10]; int i, j; printf("Enter the elements of the first matrix\n"); for(i=0; i<2; i++) for(j=0; j<2; j++) { scanf("%d", &num1[i][j]); } printf("Enter elements of the second matrix\n"); for(i=0; i<2; i++) for(j=0; j<2; j++) { scanf("%d", &num2[i][j]); } for(i=0; i<2; i++) for(j=0; j<2; j++) { sum[i][j] = num1[i][j] + num2[i][j]; } //Now Displaying the sum printf("\nSum Of the elements of the first and the second matrices are: \n"); for(i=0; i<2; i++) { for(j=0; j<2; j++) { printf("%d\t", sum[i][j]); } printf("\n"); } return 0; } |

The program above will ask for the inputs and generate its output as in the format below:

**Output**

Enter the elements of the first matrix

1

2

3

4

Enter the elements of the second matrix

1

2

3

4

Sum of the elements of the first and the second matrices are:

2 4

6 8

## Recent Comments