Searching...
Saturday 24 May 2014

PROGRAM IN LINUX C FOR MATRIX MULTIPLICATION

5/24/2014 05:34:00 pm
# include "stdio.h"
main()
{
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    printf("For frist matrix , Enter number of rows and columns : ");
    scanf("%d%d",&r1,&c1);
    printf("\nFor Second matrix , Enter number of rows and columns : ");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("\nEnter elements of First matrix row wise : \n");
       
        for(i=0;i<r1;i++)
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);

        printf("\nEnter elements of Second matrix row wise : \n");
        for(i=0;i<r2;i++)
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);

        printf("\n\n Multiplication : \n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                    mult[i][j]+=m1[i][k]*m2[k][j];
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
    return 0;

}

0 comments:

Post a Comment