C Programming Two Dimensional Array with Examples

Two-dimensional array in c programming; In this tutorial, i am going to show you what is two Dimensional Array in C and how to use two dimensional array in c.

Two Dimensional Array in C with Examples

Here, i will teach you what is two dimensional array, how to declare, initialize, and use of two dimensional with the help of examples: is as follows:

  • Two Dimensional Array Definition in C
  • Two Dimensional Array Declaration in C
  • Initialization of Two Dimensional Array
  • Example 1 – Two Dimensional Array in C
  • Example 2 – Storing elements in a matrix and printing it using 2d array in C
  • Example 3 – Find sum of all elements in a 2D (Two Dimensional) Array

Two Dimensional Array Definition in C

In c programming; two-dimensional arrays are stored as arrays of arrays. It’s is also known as matrix array.

Note that:- A matrix can be represented as a table of rows and columns.

Two Dimensional Array Declaration in C

You can use the following syntax to declare a two-dimensional array in the c programming language; as shown below:

data_type array_name[rows][columns];  

Let’s see the following example for how to declare two-dimensional array in c programming; as shown below:

int twodimen[4][3];  

Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of Two Dimensional Array

To initialize an array in c by using the index of each element. See the following easy way to initialize array in c programming; as shown below:

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Example 1 – Two Dimensional Array in C

#include<stdio.h>  
int main(){      
int i=0,j=0;    
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};     
//traversing 2D array    
for(i=0;i<4;i++){    
 for(j=0;j<3;j++){    
   printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);    
 }//end of j    
}//end of i    
return 0;  
}    

Output:

arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6

Example 2 – Storing elements in a matrix and printing it using 2d array in C

#include <stdio.h>    
void main ()    
{    
    int arr[3][3],i,j;     
    for (i=0;i<3;i++)    
    {    
        for (j=0;j<3;j++)    
        {    
            printf("Enter a[%d][%d]: ",i,j);                
            scanf("%d",&arr[i][j]);    
        }    
    }    
    printf("\n printing the elements ....\n");     
    for(i=0;i<3;i++)    
    {    
        printf("\n");    
        for (j=0;j<3;j++)    
        {    
            printf("%d\t",arr[i][j]);    
        }    
    }  
    
    return 0;  
}    

Output of the above c program; as shown below:

Enter a[0][0]: 12
Enter a[0][1]: 45
Enter a[0][2]: 63
Enter a[1][0]: 45
Enter a[1][1]: 25
Enter a[1][2]: 12
Enter a[2][0]: 74
Enter a[2][1]: 85
Enter a[2][2]: 65

printing the elements ....

12	45	63	
45	25	12	
74	85	65	

Example 3 – Find sum of all elements in a 2D (Two Dimensional) Array

Enter number of Rows :2
Enter number of Cols :2
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [2,1] : 3
Enter element [2,2] : 4
SUM of all elements : 10 

Output of the above c program; as shown below:

Enter number of Rows :2
Enter number of Cols :2
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [2,1] : 3
Enter element [2,2] : 4
SUM of all elements : 10 

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.