Saturday, 5 May 2012

Matrix Multiplication in C


#include<stdio.h>
main()
{
int n,i,j,k,r1,r2,c1,c2;
int a[10][10],b[10][10],c[10][10];
printf("Enter the order of the matrix1 and matrix2:\n");
scanf("%d %d %d %d",&r1,&c1,r2,c2);
if(c1==r2)
{
printf("Enter the elements of the matrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of the matrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Computation of product of two matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("Product Matrix Is - \n");
printf("Enter the elements of the matrix B\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&c[i][j]);
}
printf("\n");
}
}
else
{
printf("Multiplication not possible - \n");
}
getch();
return 0;
}




No comments:

Post a Comment