# 2-Dimensional (2D) Array in C Programming Language

An array is a collective name given to a group of similar variables. An array can be 1-Dimensional, **2-Dimensional**, 3-Dimensional and so on. In this topic, we will discuss **2-Dimensional (2D) arrays in C** Programming Language.

Let us understand what two dimensional arrays are. Consider the following matrix.

1 2 3 |
11 12 13 A= 14 15 16 17 18 19 |

The above-mentioned matrix is 3 by 3. The matrix elements can be accessed using the formula A[m,n], where m represents row number and n represents column number.

Thus, the first element i.e. 11 is represented as A[0,0].

Similarly,

A[0,1] = 12

A[0,2] = 13

A[1,0] = 14

A[1,1] = 15 and so on.

The **2-dimensional array** follows the similar concept. So we can declare 2-dimensional (2D) array for above matrix as A[3][3].

We can define 2-dimensional array as follows:

1 |
int A[3][3]={11,12,13,14,15,16,17,18,19} |

Element 11 can be referred as A[0][0]

Element 12 can be referred as A[0][1]

Element 13 can be referred as A[0][2]

Element 14 can be referred as A[1][0]

Element 15 can be referred as A[1][1] and so on.

Another way to define 2-D array is:

1 2 3 4 5 |
int A[3][3]={ {11,12,13}, {14,15,16}, {17,18,19} } |

The above mentioned method increases the readability of the matrix.

int A[][] and A[3][] are invalid.

We cannot skip the column index in 2-D arrays. However, int A[][3] and A[3][3] are both valid.

**Program that accept values in 2-Dimensional 3 by 3 array and displays the sum of all the elements.**

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 |
void main() { int arr[3][3], i, j, sum=0; /*Accepts input from the user and stores it in 2-D array*/ for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(“\nEnter the value for A[%d][%d]:“,i,j); scanf(“%d”,&arr[i][j]); } } /*Calculate sum of elements in 2-D array*/ for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum=sum+arr[i][j]; } } /*Display the value of sum*/ printf(“\nThe sum of the elements of 2-D array is %d”, sum); } |

**Output:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter the value for A[0][0]: 1 Enter the value for A[0][1]: 2 Enter the value for A[0][2]: 3 Enter the value for A[1][0]: 4 Enter the value for A[1][1]: 5 Enter the value for A[1][2]: 6 Enter the value for A[2][0]: 7 Enter the value for A[2][1]: 8 Enter the value for A[2][2]: 9 The sum of the elements of 2-D array is 45 |

**Explanation:**

There are two for loops. The first one accepts input from the user and stores it in 2-D array.

The second one Calculates the sum of the elements present in 2-D array.

1 2 3 4 5 6 7 8 |
for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\nEnter the value for A[%d][%d]:",i,j); scanf("%d",&arr[i][j]); } } |

Let us understand the above **for loop** iteration wise.

**1st Iteration of Outer for loop:**

Assign the value 0 to i. i.e., i=0. Condition i < 3 is true. Hence move inside for loop.

1st Iteration of Inner for loop:

The value of j=0. Condition j < 3 is true. Hence, move inside inner for loop. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[0][0].

2nd Iteration of Inner for loop:

The value of j is incremented by 1. Hence j is now equal to 1. Condition j < 3 is true. Hence, move inside inner for loop. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[0][1].

3rd Iteration of Inner for loop:

The value of j is incremented. Hence j=2. Condition j < 3 is true. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[0][2].

Now the value of j is again incremented. Hence j=3. But, the condition j < 3 is false. Hence it will exit the inner for loop and control is transferred to outer for loop.

**2nd Iteration of Outer for loop:**

The value of i is incremented by 1. Now i = 1. Condition i < 3 is true.

1st Iteration of Inner for loop:

The value of j=0. Condition j < 3 is true. Hence, move inside inner for loop. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[1][0].

2nd Iteration of Inner for loop:

The value of j is incremented by 1. Hence j is now equal to 1. Condition j < 3 is true. Hence, move inside inner for loop. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[1][1].

3rd Iteration of Inner for loop:

The Value of j is incremented. Hence j=2. Condition j < 3 is true. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[1][2].

Now the value of j is again incremented. Hence j=3. But, the condition j < 3 is false. Hence, it will exit the inner for loop and control is transferred to outer for loop.

**3rd Iteration of Outer for loop:**

The value of i = 2. Condition i < 3 is true.

1st Iteration of Inner for loop:

The value of j=0. Condition j < 3 is true. Hence, move inside inner for loop. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[2][0].

2nd Iteration of Inner for loop:

The value of j is incremented by 1. Hence j is now equal to 1. Condition j < 3 is true. Hence, move inside inner for loop. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[2][1].

3rd Iteration of Inner for loop:

The Value of j is incremented. Hence j=2. Condition j < 3 is true. Here the value entered by the user is assigned to the variable arr[i][j] i.e. arr[2][2].

Now, the value of j is again incremented. Hence, j = 3. But, the condition j < 3 will return false. Hence, it will exit this inner for loop and control is transferred to outer for loop.

Now the value of i is again incremented by 1. Hence, i = 3. But, the condition i < 3 will return false. Hence, it will exit the outer for loop as well.

Similar logic applies while calculating the addition of the array elements.

You may also want to read for loop in C, Multi dimensional array & 1-Dimensional (1D) array

mistakes in these program

Thank you pointing out the mistakes. We have made the necessary corrections.

Regards,

LearnCOnline Team

Good one buddy. Your explanation is quite impressive.

Nice and easy to understand

kinda help me in this problem please

write a program using two dimensional array that determines the lowest value among the five input values typed from the keyboard and prints the calculated sum and average

nice

nice man

Thank you. Hope this

C Tutorialhelped you… 🙂You made my task easy. This is the best way to learn C programming. You made the concept of 2D array very easy. Very nice and detailed step-by-step explanation. Thank you.

need more programs

awesome dude .wish i had a teacher like u