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 [2][3] 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 [0][0] means it represent the first (0) block of first element (0).
1 | b [2][3] |
First value of block [2] represented as 1st row &2nd value of block [3] represented as each row has three columns.
1 | b [0][0], b [1][0] |
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 [5][5]
First, [5] there are 5 blocks which represent each class as each array. Next, [5] 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<stdio.h> 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include<stdio.h> 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include<stdio.h> 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.