Monday, October 25, 2010

Matrix Multiplication Program in C

#include
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;


printf("Enter The Rows And Cloumns And Of The First Matrix:");
scanf("%d %d",&m,&n);
printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");
scanf("%d %d",&p,&q);

printf("\nEnter Elements Of The First Matrix:\n");
arrayfill(a,m,n);

printf("\nEnter Elements Of The Second Matrix:\n");
arrayfill(b,p,q);

printf("The First Matrix Is:\n");
arrayprint(a,m,n);

printf("The Second Matrix Is:\n");
arrayprint(b,p,q);
if(n!=p)
{
printf("Aborting!!!!!!/nMultiplication Of The Above Matrices Not Possible.");
exit(0);
}
else
{
for(i=0;i< m;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];
}
}
}
printf("\nMultiplication Of The Above Two Matrices Are:\n\n");
for(i=0;i< m;i++)
{
for(j=0;j< q;j++)
{
printf(" %d ",c[i][j]);
}
printf("\n");
}
}
}

arrayfill(int a[10][10],int m, int n )
{
    int i,j;
    for(i=0;i< m;i++)
    {
    for(j=0;j< n;j++)
    scanf("%d",&a[i][j]); //print the first matrix
    printf("\n");
    }
}


arrayprint(int a[10][10],int m,int n)
{
    int i,j;
    for(i=0;i< m;i++) // print the second matrix
    {
    for(j=0;j< n;j++)
    printf(" %d ",a[i][j]);
    printf("\n");
    }
}




No comments: