2-Dimensional 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 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 array for above matrix as A[3][3].

We can define 2-dimensional array as follows.
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:
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.
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.

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

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:
Value of i=0. Condition i<3 is satisfied. Hence it enters this for loop.
1st Iteration of Inner for loop:
Value of j=0. Condition j<3 is satisfied. Hence it enters this 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:
Value of j is incremented. Hence j=1. Condition j<3 is satisfied. Hence it enters this 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:
Value of j is incremented. Hence j=2. Condition j<3 is satisfied. Hence it enters this for loop.
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 not satisfied.
     Hence it exits this inner for loop.

2nd Iteration of Outer for loop:
Value of i=1. Condition i<3 is satisfied. Hence it enters this for loop.
1st Iteration of Inner for loop:
Value of j=0. Condition j<3 is satisfied. Hence it enters this 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:
Value of j is incremented. Hence j=1. Condition j<3 is satisfied. Hence it enters this 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:
Value of j is incremented. Hence j=2. Condition j<3 is satisfied. Hence it enters this for loop.
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 not satisfied.
     Hence it exits this inner for loop.  

3rd Iteration of Outer for loop:
Value of i=2. Condition i<3 is satisfied. Hence it enters this for loop.
1st Iteration of Inner for loop:
Value of j=0. Condition j<3 is satisfied. Hence it enters this 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:
Value of j is incremented. Hence j=1. Condition j<3 is satisfied. Hence it enters this 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:
Value of j is incremented. Hence j=2. Condition j<3 is satisfied. Hence it enters this for loop.
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 is not satisfied.
     Hence it exits this inner for loop.

Now the value of i is again incremented. Hence i=3. But, the condition i<3 is not satisfied. Hence it exits this outer for loop.

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


Responses

0 Respones to "2-Dimensional Array in C Programming Language"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy