Two Dimensional Arrays in C Language

This “Two Dimensional Arrays in C Language” page is giving introduction, declaration, initialization and basic programming of Two dimensional arrays using example codes.


 

Two Dimensional Arrays:

  • Two dimensional arrays are used to process two dimensional data.
  • We can process the information or 2D format like Rows and Columns
  • for example Matrix operations…

 

Syntax :
data_type <identifier>[rows][columns];

Ex :
int arr[2][2] ;
Above array declaration can store 4 elements in two dimensional form.

 

Arrays can be initialized directly in many ways :

Type-1 :
int a[2][2]= {10,20,30,40};

Type-2 :
int a[2][2] = {10,20};

Type-3 :
int a[2][2] = {
{10},
{20}
} ;

Type-4 :
int a[2][2];
a[0][0] = 10;
a[1][0] = 20;

 

  • To process the data of two dimensional or multi dimensional array elements, more than one iterator is required.
  • Outer loop represents rows to be processed
  • Inner loop represents columns to be processed

 

#include<stdio.h>
void main()
{
int arr[2][2]={10,20,30,40},i,j;
printf(“Array is\n”);
for(i=0; i<2 ; i++)
{
for(j=0; j<2 ; j++)
{
printf(“%d\t”,arr[i][j]);
}
printf(“\n”);
}
}



#include<stdio.h>
void main()
{
int arr[10][10],m,n,i,j;
printf(“row size (<=10) : “);
scanf(“%d”,&m);
printf(“column size (<=10) : “);
scanf(“%d”,&n);
printf(“Enter %d elements into an array of dimension %dX%d\n”,m*n,m,n);
for(i=0; i<m ; i++)
{
for(j=0; j<n ; j++)
{
printf(“enter ele into arr[%d][%d] : “,i,j);
scanf(“%d”,&arr[i][j]);
}
}
printf(“Array is\n”);
for(i=0; i<m ; i++)
{
for(j=0; j<n ; j++)
{
printf(“%d\t”,arr[i][j]);
}
printf(“\n”);
}
}

Share this