a c program to multiply two non-square matrix explain with suitable example
Answers
Answer:
#include<stdio.h>
void main()
{
int a[50][50],b[50][50],c[50][50],i,j,k,n,m,p,q,sum=0;
printf("Enter the number of rows and columns of matrix 1: ");
scanf("%d%d",&m,&n);
printf("Enter the number of rows and columns of matrix 2: ");
scanf("%d%d",&p,&q);
if(n==p) //condition of matrix multiplication
{
printf("Enter the elements of first matrix : \n"); //input matrix 1
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("The first matrix is:\n");//display matrix 1
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("Enter the elements of second matrix : \n"); //input matrix 2
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
printf("The second matrix is:\n"); //display matrix 2
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d ",b[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum+=(a[i][k]*b[k][j]);
}
c[i][j]=sum;
sum=0;
}
}
printf("The product matrix is:\n"); //display product matrix
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d ",c[i][j]);
printf("\n"); }
}
else
{
printf("The multiplication is not possible");
}
}
Explanation: