Write a ‘C’ program for multiplication of two mXn matrix.
#include<stdio.h>
#include<conio.h>
#define ROW 20
#define COL 20
void main()
{
int a[ROW][COL],b[ROW][COL],c[ROW][COL];
int i=0,j=0,k,sum=0,m,n;
clrscr();
printf("\n enter the m\n");
scanf("%d",&m);
printf("\n enter the n\n");
scanf("%d",&n);
printf("\n enter the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\n enter the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
printf("\n the product of matrices is as follow \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}
/* *****************************************
output
enter the m=3
enter the n=3
enter the matrix A elements
1
1
1
1
1
1
1
1
1
the matrix A elements
1 1 1
1 1 1
1 1 1
enter the matrix B elements
1
1
1
1
1
1
1
1
1
the matrix B elements
1 1 1
1 1 1
1 1 1
the product of matrices is as follow
3 3 3
3 3 3
3 3 3
**************************************** */
#include<conio.h>
#define ROW 20
#define COL 20
void main()
{
int a[ROW][COL],b[ROW][COL],c[ROW][COL];
int i=0,j=0,k,sum=0,m,n;
clrscr();
printf("\n enter the m\n");
scanf("%d",&m);
printf("\n enter the n\n");
scanf("%d",&n);
printf("\n enter the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\n enter the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
printf("\n the product of matrices is as follow \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}
/* *****************************************
output
enter the m=3
enter the n=3
enter the matrix A elements
1
1
1
1
1
1
1
1
1
the matrix A elements
1 1 1
1 1 1
1 1 1
enter the matrix B elements
1
1
1
1
1
1
1
1
1
the matrix B elements
1 1 1
1 1 1
1 1 1
the product of matrices is as follow
3 3 3
3 3 3
3 3 3
**************************************** */
Could you please help to post algorithm of this program??? it helped me a lot. Thank you.
ReplyDeleteThanx really helpful.... :)
ReplyDelete