Write a program to perform multiplication of two matrices by making function matmul()

Leave a Comment
#include<stdio.h>
#include<conio.h>
void matmul(int mat1[][100], int mat2[][100],int m, int n,int p, int q, int ans[][100])
{
int i,j,k;
for(i=0;i {
for(j=0;j {
ans[i][j]=0;
for(k=0;k {
ans[i][j]=ans[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
}


int main()
{
int mat1[100][100],mat2[100][100],ans[100][100],m,n,p,q,i,j;
printf("\nEnter the size of the 1st matrix as mxn:");
scanf("%d%d", &m,&n);
printf("\nEnter the size of the 2nd matrix as pxq:");
scanf("%d%d", &p,&q);
if(n!=p)
{
printf("\nThe matrx cant be multiplied!!!!");
printf("\nAborting!!!!");
getch();
exit(0);
}
printf("\nEnter for the first matrix:\n");
for(i=0;i<n;i++)

{
for(j=0;j<m;j++) {
printf("\nEnter the element:");
scanf("%d",&mat1[i][j]);
}
}
printf("\nEnter for the second matrix:\n");
for(
i=0;i<n;i++)
{
for(j=0;j<m;j++) 
{
printf("\nEnter the element:");
scanf("%d",&mat2[i][j]);
}
}
matmul(mat1,mat2,m,n,p,q,ans);
printf("\nThe resultant matrix is\n");
for(i=0;i {
for(j=0;j {
printf("%d\t",ans[i][j]);
}
printf("\n");
}
getch();
}

0 comments: