Program 5: Implement Matrix Multiplication and Validate The Rules of Multiplication. Algorithm
Program 5: Implement Matrix Multiplication and Validate The Rules of Multiplication. Algorithm
ALGORITHM:
Step 1:start
Step 3: check for i=0 to M and j=0 to N for the matrix A,read A[i][j]
Step 4:Check for i=0to P and j=0 to Q for matrix B read B[i][j]
Step 9:stop
GNDEC/CPL
SUB CODE: 21CPL17/27
FLOWCHART:
GNDEC/CPL
SUB CODE: 21CPL17/27
GNDEC/CPL
SUB CODE: 21CPL17/27
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int a[20][20],b[20][20],c[20][20];
int m,n,p,q,i,j,k;
clrscr();
printf("enter rows and columns of matrix a\n");
scanf("%d%d",&m,&n);
printf("enter rows and columns of matrix b\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("matrix multiplication not possible\n");
return 0;
}
printf("enter elements of matrix a\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter elements of matrix b\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
GNDEC/CPL
SUB CODE: 21CPL17/27
}
}
printf("product of two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\n",c[i][j]);
}
}
getch();
}
GNDEC/CPL