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.

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:

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:

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.

Output:

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.

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

You may also like...

12 Responses

  1. ali says:

    mistakes in these program

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

  6. purvi sharma says:

    need more programs

  1. March 1, 2014

    […] 2-Dimensional Array […]

  2. March 1, 2014

    […] 2-Dimensional Array […]

Leave a Reply

Your email address will not be published. Required fields are marked *