Multidimensional Arrays:
C programming language allows the user to create arrays of arrays known as multidimensional arrays. To access a particular element from the array we have to use two subscripts one for row number and other for column number.
The notation is of the form array[i][j] where i stands for row subscripts and j stands for column subscripts.
The array holds i*j elements. Suppose there is a multidimensional array array[i][j][k][m]. Then this array can hold i*j*k*m numbers of data. In the same way, the array of any dimension can be initialized in C programming.
For example :
int matrix[3][4];
Here, matrix is an array of two dimension, which is an example of multidimensional array. This array has 3 rows and 4columns. For better understanding of multidimensional arrays, array elements of above example can be as below:
Matrix
[3][4]
|
Column-1
|
Column-2
|
Column-3
|
Column-4
|
Row-1
|
Matrix[0][0]
|
Matrix[0][1]
|
Matrix[0][2]
|
Matrix[0][3]
|
Row-2
|
Matrix[1][0]
|
Matrix[1][1]
|
Matrix[1][2]
|
Matrix[1][3]
|
Row-3
|
Matrix[2][0]
|
Matrix[2][1]
|
Matrix[2][2]
|
Matrix[2][3]
|
Initialization of Multidimensional Arrays:
In C, multidimensional arrays can be initialized in the different number of ways.
int matrix[3][4]={{1,2,3,4},{11,22,33,44},{111,222,333,444}};
OR
int matrix[][4]={{1,2,3,4},{11,22,33,44},{111,222,333,444}};
OR
int matrix[3][4]={1,2,3,4,11,22,33,44,111,222,333,444};
In C programming, arrays can be accessed and treated like variables in C.
For example:
scanf("%d",&matrix[2][3]);
printf("%d",matrix[2][3]);
Arrays can be accessed and updated using its index.
Example: C program to add two matrices.
#include <stdio.h>
int main()
{ int mat1[3][4],mat2[3][4],sum[3][4],row,col;
printf("\nEnter elements of 1st matrix:\n");
for(int row=0;row<3;row++)
{ for(col=0;col<4;col++)
{ printf("Enter element mat1[%d][%d]: ",row,col);
scanf("%d",&mat1[row][col]);
}
}
printf("Enter elements of 2nd matrix:\n");
for(int row=0;row<3;row++)
{ for(col=0;col<4;col++)
{ printf("Enter element mat2[%d][%d]: ",row,col);
scanf("%d",&mat2[row][col]);
}
}
/*Adding Two matrices */
for(int row=0;row<3;row++)
{ for(col=0;col<4;col++)
{sum[row][col]=mat1[row][col]+mat2[row][col];
}
}
/* Displaying the resultant sum matrix. */
printf("Matrix-1:\n");
for(int row=0;row<3;row++)
{ for(col=0;col<4;col++)
{ printf("%d\t",mat1[row][col]);
}
printf("\n");
}
printf("Matrix-2:\n");
for(int row=0;row<3;row++)
{ for(col=0;col<4;col++)
{ printf("%d\t",mat2[row][col]);
}
printf("\n");
}
printf("Sum:\n");
for(int row=0;row<3;row++)
{ for(col=0;col<4;col++)
{ printf("%d\t",sum[row][col]);
}
printf("\n");
}
}
Output:
Enter elements of 1st matrix:
Enter element mat1[0][0]: 1
Enter element mat1[0][1]: 2
Enter element mat1[0][1]: 2
Enter element mat1[0][2]: 1
Enter element mat1[0][3]: 4
Enter element mat1[1][0]: 2
Enter element mat1[1][1]: 2
Enter element mat1[1][2]: 0
Enter element mat1[1][3]: 0
Enter element mat1[2][0]: 1
Enter element mat1[2][1]: 0
Enter element mat1[2][2]: 4
Enter element mat1[2][3]: 2
Enter elements of 2nd matrix:
Enter element mat2[0][0]: 1
Enter element mat2[0][1]: 2
Enter element mat2[0][2]: 3
Enter element mat2[0][3]: 2
Enter element mat2[1][0]: 0
Enter element mat2[1][1]: 1
Enter element mat2[1][2]: 0
Enter element mat2[1][3]: 2
Enter element mat2[2][0]: 3
Enter element mat2[2][1]: 1
Enter element mat2[2][2]: 0
Enter element mat2[2][3]: 2
Matrix-1:
1 2 1 4
2 2 0 0
1 0 4 2
Matrix-2:
1 2 3 2
0 1 0 2
3 1 0 2
Sum:
2 4 4 6
2 3 0 2
4 1 4 4
No comments:
Post a Comment