Wednesday, 13 August 2014

\\MATRIX MULTIPLICATION
\\CODE

#include<stdio.h>
#include<conio.h>
clrscr();
int main()
{
    int a[10][10],b[10][10],s[10][10];
    int sum=0,i,j,k,r1,c1,r2,c2;
    printf("\n Enter the rows of 1st matrix:");
    scanf("\n %d",&r1);
    printf("\n Enter the columns of 1st matrix:");
    scanf("\n %d",&c1);
    printf("\n Enter the rows of 2nd matrix:");
    scanf("\n %d",&r2);
    printf("\n Enter the columns of 2nd matrix:");
    scanf("\n %d",&c2);
    if(r2==c1)
    {
        printf("\n Enter the matrix a:\n ");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        printf("\n Enter the matrix b:\n ");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
                scanf("%d",&b[i][j]);
            }
        }
    printf("\n The matrix a is as follows:\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            printf(" \t %d",a[i][j]);
        }
    printf("\n");
    }
    printf("\n The matrix b is as follows:\n");
    for(i=0;i<r2;i++)
    {
        for(j=0;j<c2;j++)
        {
            printf(" \t %d",b[i][j]);
        }
    printf("\n");
    }
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c2;j++)
        {
            sum=0;
            for(k=0;k<c2;k++)
            {
                sum=sum+(a[i][k]*b[k][j]);
            }
                        s[i][j]=sum;
        }
    }
        printf("The resultant matrix is: \n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                printf("\t %d",s[i][j]);
            }
                printf("\n");
        }
}
        else
    {
        printf("\n Matrix cannot be multiplied...Try again");
    }
        getch();
        return 0;
}
\\OUTPUT

Enter the rows of 1st matrix:2
Enter the columns of 1st matrix:3
Enter the rows of 2nd matrix:3
Enter the columns of 2nd matrix:2
 Enter the matrix a:
4
8
9
7
5
6
Enter the matrix b:
7
4
6
8
1
2

The matrix a is as follows:
          4   8   9
          7   5   6

The matrix b is as follows:
          7     4
          6     8
          1     2
The resultant matrix is:
          76    80
          79    68



 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.