Saturday 15 November 2014

MATRIX MULTIPLICATION

#include<stdio.h>
main()
{
int r1,c1;
printf("Enter the total number of rows for matrix 1\n");
scanf("%d",&r1);
printf("Enter the total number of column for matrix 1\n");
scanf("%d",&c1);
int a[r1][c1];
int r2,c2;
a:
printf("Enter the total number of rows for matrix 2\n");
scanf("%d",&r2);
if(r2!=c1)
{
    printf("Note:For matrix multiplication total number of coloumns for matrix 1 must be same as total number of rows for matrix 2\nPlease enter again\n");
    goto a;
}
printf("Enter the total number of columns for matrix 2\n");
scanf("%d",&c2);
int
b[r2][c2];
int i,j;
printf("ENTER THE VALUES FOR MATRIX 1\n");
for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
{
    printf("Enter value of row %d and column %d\t",i+1,j+1);
    scanf("%d",&a[i][j]);
}
printf("ENTER THE VALUES FOR MATRIX 2\n");
for(i=0;i<r2;i++)
    for(j=0;j<c2;j++)
{
    printf("Enter value of row %d and column %d\t",i+1,j+1);
    scanf("%d",&b[i][j]);
}
int k;
int res[r1][c2];
for(i=0;i<r1;i++)
    for(j=0;j<c2;j++)
{
    res[i][j]=0;
    for(k=0;k<c1;k++)
    {
        res[i][j]+=(a[i][k]*b[k][j]);
    }
}
printf("\nMATRIX 1\n");
for(i=0;i<r1;i++)
    {
    for(j=0;j<c1;j++)
{
    printf("%d ",a[i][j]);
}
printf("\n");
    }
printf("\nMATRIX 2\n");
for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++)
{
    printf("%d ",b[i][j]);
}
printf("\n");
    }
    printf("\nPRODUCT MATRIX \n");
for(i=0;i<r1;i++)
    {
    for(j=0;j<c2;j++)
{
    printf("%d ",res[i][j]);
}
printf("\n");
    }
}

No comments:

Post a Comment