Wednesday, June 1, 2011

To Perform Matrix Multiplication in C

0 comments
/*To perform matrix multiplication*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int p,q,m,n,i,j,k;
int a[25][25],b[25][25],c[25][25];
/*Enter the order of matrices*/
printf("Enter the order of matrix A (m,n)\n");
scanf("%d%d",&m,&n);
printf("Enter order of matrix B (p,q) \n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("matrices not compatible");
}
if(n==p)
{
/*Enter the elements of matrices*/
printf("enter the elements of matrix A");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf(%d",&a[i][j]);
}
}
printf("enter the elements of B");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The product of two matrices A&B \n");
for(i=1;i<=m;i=i+1)
{
for(j=1;j<=q;j=j+1)
{
for(k=1;k<=n;k=k+1)
{
c[i][j]=0;
}
}
}
for(i=1;i<=m;i=i+1)
{
j=1;j<=q;j=j+1)
{
for(k=1;K<=n;k=k+1)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
printf("%d",c[i][j]);
}
printf("%d",c[i][j]);
}
}
getch();
}


Click here for Algorithm of Matrix multiplication
Continue reading →