Tuesday, December 6, 2011

Write a C Program to Multiplication of two matrices

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

int m1,n1,m2,n2,i,j,k,z[10][10]={0};

void value_sub(int a,int b,int arr[][10] )
{    for(i=0;i<a;i++)
    {    for(j=0;j<b;j++)
        {    printf(“Mat[%d%d] = “,i+1,j+1);
            scanf(“%d”,&arr[i][j]);
            fflush(stdin);
        }
        printf(“”);
    }
}

void mat_mul(int a,int b,int arr[][10],int brr[][10])
{    int k=0;
    for(i=0;i<a;i++)
    {    for(j=0;j<b;j++)
        {    for(k=0;k<a;k++)
            {    z[i][j]+=arr[i][k]*brr[k][j];
                printf(“%d\t”,z[i][j]);
            }
            printf(“\n\n”);
        }
    }
}

int main()
{    int A[10][10]={0},B[10][10]={0};
    printf(“Enter the column and row of first matrix(m x n)\n”);
    scanf(“%d%d”,&m1,&n1);
    printf(“Enter the column and row of second matrix(m x n)\n”);
    scanf(“%d%d”,&m2,&n2);
    printf(“\n\n”);
    if (n1==m2)
    {    value_sub(m1,n1,A);
        printf(“\n\n”);
        value_sub(m2,n2,B);
        printf(“\n\n”);
        mat_mul(m1,n2,A,B);
    }
    else
        printf(“Matrix multiplication cannot be done”);
    getch();
}

No comments:

Post a Comment