Declaration of Two Dimensional Array in C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Two Dimensional Array in C

The two-dimensional array can be defined as an array of arrays. The 2D array is


organized as matrices which can be represented as the collection of rows and columns.

Declaration of two dimensional Array in C

The syntax to declare the 2D array is given below.

1. data_type array_name[rows][columns];  

Consider the following example.

1. int twodimen[4][3];  

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

Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously. However, this will not work with 2D arrays.
We will have to define at least the second dimension of the array. The two-dimensional
array can be declared and defined in the following way.

1. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};  

Two-dimensional array example in C


1. #include<stdio.h>  
2. int main(){      
3. int i=0,j=0;    
4. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};     
5. //traversing 2D array    
6. for(i=0;i<4;i++){    
7.  for(j=0;j<3;j++){    
8.    printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);    
9.  }//end of j    
10. }//end of i    
11. return 0;  
12. }    
C 2D array example: Storing elements in a matrix and printing it.
1. #include <stdio.h>    
2. void main ()    
3. {    
4.     int arr[3][3],i,j;     
5.     for (i=0;i<3;i++)    
6.     {    
7.         for (j=0;j<3;j++)    
8.         {    
9.             printf("Enter a[%d][%d]: ",i,j);                
10.             scanf("%d",&arr[i][j]);    
11.         }    
12.     }    
13.     printf("\n printing the elements ....\n");     
14.     for(i=0;i<3;i++)    
15.     {    
16.         printf("\n");    
17.         for (j=0;j<3;j++)    
18.         {    
19.             printf("%d\t",arr[i][j]);    
20.         }    
21.     }    
22. }    
\* C Program to to Add Two Matrix Using Multi-dimensional Arrays *\

# include < stdio.h >


int  main( )
{
int  a[10][10], b[10][10], sum[10][10], i, j, r, c ;
printf(" Enter the Numbers of Row : ") ;
scanf("%d ",& r) ;
printf("\n Enter the Number of Column : ") ;
scanf("%d ",& c) ;
printf("\n Enter the Element of First Matrix : \n") ;
for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; i++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& a[i][j]) ;
}
}

printf("\n Enter the Element of Second Matrix : \n") ;

for (  i = 0 ; i < r ; i++)


{
for (  j = 0 ; j < c ; i++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& b[i][j]) ;
}
}
for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; i++)
sum[i][j] = a[i][j] + b[i][j] ;
}
printf("\n\n Sum of Two Matrix are : \n") ;
for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; i++)
{
printf("\t %d ",  sum[i][j]) ;
}
printf(" \n ") ;
}
return ( 0 ) ;
}
Output of Program:
\* C Program to to Multiply to Matrix Using Multi-dimensional Arrays *\

# include < stdio.h >


int  main( )
{
int  a[10][10], b[10][10], mul[10][10], i, j, k, r, c, sum ;
printf(" Enter the Numbers of Row : ") ;
scanf("%d ",& r) ;
printf("\n Enter the Number of Coloumn : ") ;
scanf("%d ",& c) ;

printf("\n Enter the Element of First Matrix : \n") ;


for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; i++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& a[i][j]) ;
}
}

printf("\n Enter the Element of Second Matrix : \n") ;


for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; i++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& b[i][j]) ;
}
}

for (  i = 0 ; i < r ; i++)


{
for (  j = 0 ; j < c ; i++)
{
sum = 0 ;
for (  k = 0 ; k < r ; k++)
{
sum = sum + ( a[i][k] * b[k][j] ) ;
}
mul[i][j] = sum ;
}
}

printf("\n\n Multiplicaiton of Two Matrix are : \n") ;


for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; i++)
{
printf("\t %d ",  mul[i][j]) ;
}
printf(" \n ") ;
}

Output of Program:
\* C Program to to Find Transpose of a Matrix *\

# include < stdio.h >


int  main( )
{
int  a[10][10], i, j, r, c ;
printf(" Enter the Numbers of Row : ") ;
scanf("%d ",& r) ;
printf("\n Enter the Number of Column : ") ;
scanf("%d ",& c) ;

printf("\n Enter the Element of Matrix : \n") ;


for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; j++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& a[i][j]) ;
}
}
printf("\n\n Element in the Matrix are : \n") ;
for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; j++)
{
printf("\t %d ",  a[i][j]) ;
}
printf(" \n ") ;
}
printf("\n\n Transpose of a Matrix : \n") ;
for (  i = 0 ; i < r ; i++)
{
for (  j = 0 ; j < c ; j++)
{
printf("\t %d ",  a[j][i]) ;
}
printf(" \n ") ;
}
return ( 0 ) ;
}
Output of Program:

You might also like