Wednesday 11 January 2012

Write a c program for Matrix multiplication

#include<stdio.h>
#include<conio.h>

void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,k,o,p,sum=0;
clrscr();
printf("Enter the order of the 1st matrix::\n");
scanf("%d%d",&m,&n);
printf("Enter the order of the 2nd matrix::\n");
scanf("%d%d",&o,&p);
printf("Enter the 1st matrix::\n");
for(i=0;i<m;i++)
{
    for(j=0;j<n;j++)
    {
        scanf("%d",&a[i][j]);
    }
}

printf("Enter the 2nd matrix::\n");
for(i=0;i<o;i++)
{
    for(j=0;j<p;j++)
    {
        scanf("%d",&b[i][j]);
    }
}

if(n!=o)
{
printf("Matrix multiplication is not possible::\n");
}
else
{
for(i=0;i<m;i++)
{
    for(j=0;j<p;j++)
    {
        c[i][j]=0;
    }
}
for(i=0;i<m;i++)
{
    for(j=0;j<p;j++)
    {
        sum=0;
        for(k=0;k<n;k++)
        {
            sum=sum+a[i][k]*b[k][j];
            c[i][j]=sum;
        }
    }
}
printf("Result is=\n");
for(i=0;i<m;i++)
{
    for(j=0;j<p;j++)
    {
        printf("%d  ",c[i][j]);
    }
    printf("\n");
}
}
getch();
}

No comments:

Post a Comment