After discussing the Fundamentals of 2-D Arrays in C programming, it is necessary to learn how to perform operations on such types of arrays. Handling multiple dimensions in an array needs practice and basic knowledge about Decision Control and Looping statements.
Some of the basic operations that we'll perform are :
**************************************
TRANSPOSE OF A MATRIX OR 2-D ARRAY
#include<stdio.h>
int main()
{
/* 2D array declaration*/
int a[3][3],tran[3][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<3; i++) {
for(j=0;j<3;j++) {
printf("Enter value for a[%d][%d]:", i, j);
scanf("%d", &a[i][j]);
}
}
//transposing the array elements
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
tran[i][j] = a[j][i] ;
}
}
//printing the 2D array
printf("Transpose of the 3*3 matrix is :\n");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
printf("%d ", tran[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT :
Enter value for a[0][0]:1
Enter value for a[0][1]:2
Enter value for a[0][2]:3
Enter value for a[1][0]:4
Enter value for a[1][1]:5
Enter value for a[1][2]:6
Enter value for a[2][0]:7
Enter value for a[2][1]:8
Enter value for a[2][2]:9
Transpose of the 3*3 matrix is :
1 4 7
2 5 8
3 6 9
**************************************
SUM OF TWO 2-D ARRAYS
#include<stdio.h>
int main()
{
int a[3][3] , b[3][3] , sum[3][3] ;
int i , j;
printf("Enter Matrix 1 :");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
scanf(" %d ", a[i][j]);
}
}
printf("Enter Matrix 2 :");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
scanf(" %d ", b[i][j]);
}
}
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum of the matrices is : \n");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
printf(" %d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT :
Enter Matrix 1 : 1 2 3 4 5 6 7 8 9
Enter Matrix 2 : 12 -1 2 -30 10 0 1 32 1
Sum of the matrices is :
13 1 5
-26 15 6
8 40 10
**************************************
PRODUCT OF TWO 2-D ARRAYS
#include <stdio.h>
int main()
{
int r1, c1, r2 ,c2, i, j, k, sum = 0;
int a[10][10], b[10][10], m[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter elements of first matrix\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &r2, &c2);
printf("Enter elements of second matrix\n");
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
if (c1 != r2)
printf("The multiplication isn't possible.\n");
else
{
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < r2 ; k++) {
sum = sum + a[i][k]*b[k][j];
}
m[i][j] = sum;
sum = 0;
}
}
}
printf("Matrix after multiplication is :\n");
for (i = 0; i < r1; i++){
for (j = 0; j < c2; j++){
scanf("%d", m[i][j])
}
printf("\n");
}
return 0;
}
OUTPUT :
Enter number of rows and columns of first matrix
3 3
Enter elements of first matrix
1 2 0 0 1 1 2 0 1
Enter number of rows and columns of second matrix
3 3
Enter elements of second matrix
1 1 2 2 1 1 1 2 1
Matrix after multiplication is :
5 3 4
3 3 2
3 4 5