C Program to Multiply Two Matrices Using 2D Arrays
Introduction:
Matrix multiplication is a fundamental operation in mathematics and is used in a wide range of fields, including physics, engineering, and computer science. In programming, we can use 2D arrays to represent matrices and perform matrix multiplication operations. In this article, we will discuss a C Program to Multiply Two Matrices Using 2D Arrays.
Now let's look at the program in detail:
#include<stdio.h>
#include<conio.h>
main()
{
int a[2][3],b[3][2],prod[2][2];
int i,j,k;
printf("enter the elements for matrix first\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements for matrix second\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
/* process for matrix multiplication */
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
prod[i][j]=0;
for(k=0;k<3;k++)
{
prod[i][j]=prod[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n The product of the two matrix is :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",prod[i][j]);
}
printf("\n");
}
getch();
}
Output:
Conclusion:
In conclusion, we have seen how to C Program to Multiply Two Matrices Using 2D Arrays. We learned that matrix multiplication involves multiplying each element of the ith row of matrix A by the corresponding element of the jth column of matrix B and summing the results. We also saw how to implement this logic using for loops and 2D arrays in C. By understanding this program, you can easily perform matrix multiplication in your own projects and applications.
You can also Read:
No comments:
Post a Comment