C Programming

# Two-Dimensional Array in C

Two-dimensional array is nothing but extension of one-dimensional array. Like array, it holds some same type of data element in a different manner. It holds all the properties as single dimension array has. Now, we see how two-dimensional arrays are declared in a C programming.

## Declaring a two-dimensional array:

 1 int b  so the number of variables = (2*3) = 6

One array called has two blocks. Each block has also three blocks just like 0, 1, and 2.

Two-dimensional array is nothing but our perception or another physical representation.

2D array is also called array of array because b [][] is an array. There are two blocks inside the array. They are also array because each block in the two-dimensional array. It has also three variables treated as array.

## How can we access these variables?

We just write, b  means it represent the first (0) block of first element (0). 1 b 

First value of block  represented as 1st row &2nd value of block  represented as each row has three columns.

 1 b , b 

here we can also access these variables in same way.

## Where do we use Two-dimensional array?

Suppose in a school, there are 5 classes. Each class have 5 students. We must determine the value of all the students of each class. In this case, we must use 2D array.     S 

First,  there are 5 blocks which represent each class as each array. Next,  represents each class that have 5 students.

## Example-1:

Here, we see an example of two-dimensional arrays. With the help of 2 D array, we can see here sum of two matrices.

 12345678910111213141516171819202122232425262728293031 #include int main() {     int a [ 3 ] [ 3 ] , b [ 3 ] [ 3 ] , c [ 3 ] [ 3 ] , i , j ; // declaring two dimensional array.     printf ( " Enter 9 numbers for first matrix \n " ) ;     for ( i = 0 ; i <= 2 ; i++ )     {         for ( j = 0 ; j <= 2 ; j++ )         {             scanf("%d",&a[i][j]) ;      // initializing values to the 1st matrix.         }     }     printf ( " Enter 9 numbers for second matrix \n " ) ;     for ( i = 0 ; i <= 2 ; i++ )     {         for ( j = 0 ; j <= 2 ; j++ )         {             scanf("%d",&b[i][j]) ;      // initializing values to the 2nd  matrix.         }     }     for ( i = 0 ; i <= 2 ; i++ )     {         for ( j = 0 ; j <= 2 ; j++ )         {             c [ i ] [ j ] = a [ i ] [ j ] + b [ i ] [ j ] ; // sum of two matrices.             printf ( " %d \t " ,   c [ i ] [ j ] ) ;         }         printf ( " \n " ) ;     }     return 0 ; }

## Output: ## Explanation:

Here, we declare two dimensional arrays (matrices) to take some inputs from the user. This matrices are a [][] and b [][]. Using loops, we enter some values from the users to these matrices. Now, we sum up these elements according to the mathematical rules of matrices and print the result to the monitor.

## Example-2:

Here, we see another example of two-dimensional arrays. In Mathematics, matrix acts as a two-dimensional array. Here, we want to transpose a matrix.

 123456789101112131415161718192021222324252627282930313233343536373839404142 #include int main() {     int m , n , i , j , c [ 100 ] [ 100 ] , a [ 100 ] [ 100 ] ; // declaring two dimensional array.     printf ( " Enter the number of rows and columns of matrix : " ) ;     scanf("%d%d",&m,&n);     for ( i = 0 ; i < m ; i++ )     {         for ( j = 0 ; j < n ; j++ )         {             printf ( " Enter the element_[%d][%d] : " , i , j ) ;             scanf("%d",&c[i][j]);   // entering values in 2 D array.         }     }     printf ( " \n THE ORIGINAL MATRIX IS : \n " ) ;     for ( i = 0 ; i < m ; i++ )     {         for ( j = 0 ; j < n ; j++ )         {             printf ( " %d \t ", c [ i ] [ j ] ) ;         }         printf ( " \n  " ) ;     }     for ( i = 0 ; i < m ; i++ )     {         for (  j = 0 ; j < n ; j++ )         {             a [ j ] [ i ]   =   c [ i ] [ j ] ;         }     }     printf ( " \n TRANSPOSE OF THE GIVEN MATRIX IS GIVEN BELOW : \n " ) ;     for ( i = 0 ; i < n ; i++ )     {         for ( j = 0 ; j < m ; j++ )         {             printf ( " %d \t " , a [ i ] [ j ] ) ;      // transposing the matrix.         }         printf ( " \n " ) ;     }     return 0 ; }

## Output: ## Explanation:

Here, we want to transpose a given matrix. First, we declare a two-dimensional array. As we said earlier, each matrix in mathematics acts as a two-dimensional array. So, the declaring array is now transposed using for loops.

## Example-3:

Here, we see an example of two-dimensional arrays. With the help of 2 D array, we can see here multiplication of two matrices.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 #include int main() {     int a [ 10 ] [ 10 ] , b [ 10 ] [ 10 ] , mul [ 10 ] [ 10 ] , r , c , i , j , k ; // declaring array.     printf ( " enter the number of row = " ) ;     scanf("%d",&r);     printf ( " enter the number of column = " ) ;     scanf("%d",&c);     printf ( " enter the first matrix element = \n " ) ;     for ( i = 0 ; i < r ; i++ )     {         for ( j = 0 ; j < c ; j++ )         {             scanf("%d",&a[i][j]);         }     }     printf ( " enter the second matrix element = \n " ) ;     for ( i = 0 ; i < r ; i++ )     {         for ( j = 0 ; j < c ; j++ )         {             scanf("%d",&b[i][j]);         } }     printf ( " multiply of the matrix = \n " ) ;     for( i = 0 ; i < r ; i++ )     {         for ( j = 0 ; j < c ; j++ )         {             mul [ i ] [ j ] = 0 ;                 for( k = 0 ; k < c ; k++ )                 {                     mul [ i ] [ j ]+= a [ i ] [ k ] * b [ k ] [ j ] ;   // multiplying the values.                 }         }     }     //for printing result     for ( i = 0 ; i < r ; i++ )     {         for ( j = 0 ; j < c ; j++ )         {             printf ( " %d \t " , mul [ i ] [ j ] ) ;         }         printf ( " \n " ) ;     }     return 0 ; }

## Output: ## Explanation:

Here, we declare two dimensional arrays (matrices) to take some inputs from the user. This matrices are a [][] and b [][]. Using loops, we enter some values from the users to these matrices. Now, we multiply these elements according to the mathematical rules of matrices and print the result to the monitor.

## Conclusion:

Two-dimensional array is a form of matrix in mathematics. With the help of two-dimensional arrays, we can easily solve different types of mathematical matrix related problem. Sparse matrix is an example of two-dimensional arrays. 