Tuesday, March 29, 2016

C program to implement multiplication between two 2D matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,m,n,a[20][20],b[20][20],c[20][20],i,j,k,sum;
printf("enter dimension of first matrix");
scanf("%d%d",&m,&n);
printf("enter values of first matrix");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter dimension of second matrix");
scanf("%d%d",&p,&q);
printf("enter elements of 2nd matrix");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
sum=0;
for(k=1;k<=n;k++)
{
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("multiplication is ");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=q;j++)
{
printf("%d ",c[i][j]);
}
}
getch();
}
output
Enter dimension of first matrix 2 3
enter values of first matrix 1 2 3 4 5 6
Enter dimension of second matrix 3 2
enter values of 2nd matix 1 2 3 4 5 6
multiplication is
22 28
49 64

No comments:

Post a Comment