F -X C h a n ge F -X C h a n ge N y bu 2. Multi dimensional array 2.1 Two dimensional array Two dimensional array is nothing but array of array. syntax of array: data_type array_name[num_of_rows][num_of_column]; S.no Array declaration Array initialization Accessing array 1 Syntax: data_type arr_name [num_of_rows][num_of_column]; data_type arr_name[2][2] = {{0,0},{0,1},{1,0},{1,1}}; arr_name[index]; int arr[2][2] = {1,2, 3, 4}; arr [0] [0] = 1; arr [0] ]1] = 2; arr [1][0] = 3; arr [1] [1] = 4; 2 Example: int arr[2][2]; Example program for two dimensional array in C: Source Code to initialize and access two-dimensional array: #include<stdio.h> int main() { int i,j; // declaring and Initializing array int arr[2][2] = {10,20,30,40}; /* Above array can be initialized as below also arr[0][0] = 10; // Initializing array arr[0][1] = 20; arr[1][0] = 30; arr[1][1] = 40; */ for (i=0;i<2;i++) { for (j=0;j<2;j++) { // Accessing variables printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]); } } } ac .c tr om k lic C om k lic C .c re . . k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! PD O W ! PD k e r- s o ft w a re F -X C h a n ge F -X C h a n ge N y bu Output: value of arr[0] [0] is 10 value of arr[0] [1] is 20 value of arr[1] [0] is 30 value of arr[1] [1] is 40 Source code to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix entered by user. #include <stdio.h> int main(){ float a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrix\n"); /* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/ for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter a%d%d: ",i+1,j+1); scanf("%f",&a[i][j]); } printf("Enter the elements of 2nd matrix\n"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } for(i=0;i<2;++i) for(j=0;j<2;++j){ /* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ } printf("Sum Of Matrix:\n"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("%.1f\t",c[i][j]); if(j==1) /* To display matrix sum in order. */ printf("\n"); } return 0; } Output Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter the elements of 1st matrix a11: 2; a12: 0.5; a21: -1.1; a22: 2; the elements of 2nd matrix b11: 0.2; b12: 0; b21: 0.23; b22: 23; Sum Of Matrix: 2.2 0.5 -0.9 25.0 ac .c tr om k lic C om k lic C .c re . . k e r- s o ft w a w w ac ww ww tr to to bu y N O W ! PD O W ! PD k e r- s o ft w a re