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

```    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.

We can define 2-dimensional array as follows:

```int A={11,12,13,14,15,16,17,18,19}
```

Element 11 can be referred as A
Element 12 can be referred as A
Element 13 can be referred as A
Element 14 can be referred as A
Element 15 can be referred as A and so on.

Another way to define 2-D array is:

```int A={
{11,12,13},
{14,15,16},
{17,18,19}
}```

The above mentioned method increases the readability of the matrix.

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

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

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

```void main()
{
int arr, 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:

```Enter the value for A: 1

Enter the value for A: 2

Enter the value for A: 3

Enter the value for A: 4

Enter the value for A: 5

Enter the value for A: 6

Enter the value for A: 7

Enter the value for A: 8

Enter the value for A: 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.

```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.
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.
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.
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.
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.
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.
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.
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.
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.

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

### 14 Responses

1. ali says:

mistakes in these program

• Prashant N. says:

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

Regards,
LearnCOnline Team

2. Durga Swaroop says:

Good one buddy. Your explanation is quite impressive.

3. louann says:

Nice and easy to understand

4. rexon says:

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

5. aravind says:

nice

6. aravind says:

nice man

• learnconline says:

Thank you. Hope this C Tutorial helped you… 🙂

7. Manesh says:

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.

8. purvi sharma says:

need more programs

9. Juggernaut says:

awesome dude .wish i had a teacher like u

1. March 1, 2014

[…] 2-Dimensional Array […]

2. March 1, 2014

[…] 2-Dimensional Array […]

3. July 8, 2020

[…] 2-Dimensional (2D) Array […] ## FREE C Programming Cheatsheet

Cheatsheet for a quick glance at C programming concepts and syntax