Tuesday 14 February 2012

how to multipli two matrix


#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k;
printf("enter the order of first matrix(m,n):");
scanf("%d %d",&m,&n);
printf("enter the order of second matrix(p,q):");
scanf("%d %d",&p,&q);
if(n!=p)
printf("multiplication  is not possible");
else
{
printf("enter the elements of first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter elelmets(%d,%d)\n",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nenter the elemets of second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("enter elelmets of (%d,%d)",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);

}
printf("\n");
}
printf("second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d",b[i][j]);

}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("multiplication of matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("\t%d",c[i][j]);
printf("\n");
}
}
}

No comments:

Post a Comment