Wednesday, July 20, 2011

Program to multiply two matrices

/*Serial No.89     [swami64.cpp]*/

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10],mat2[10][10],mat3[10][10];
int i,j,k,r1,r2,c2,c1;
clrscr();
printf("Enter the no. of rows and column of first matrix:");
scanf("%d%d",&r1,&c1);
printf("\nEnter the no. of rows and column of second matrix:");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
    {
    printf("Matrix multiplication is not possible!");
    getch();
    exit(0);
    }
printf("\nenter the 1st matrix\n");
for(i=0;i<r1;i++)
    {
    for(j=0;j<c1;j++)
        {
        scanf("%d",&mat1[i][j]);
        }
    }
printf("\n\n\nFirst Entered matrix is:\n");
for(i=0;i<r1;i++)
    {
    printf("\n");
    for(j=0;j<c1;j++)
        {
        printf("%d\t",mat1[i][j]);
        }
    }
printf("\n\nEnter the 2nd matrix\n");
for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++)
        {
        scanf("%d",&mat2[i][j]);
        }
    }
printf("\n\n\nSecond Entered matrix is:\n");
for(i=0;i<r2;i++)
    {
    printf("\n");
    for(j=0;j<c2;j++)
        {
        printf("%d\t",mat2[i][j]);
        }
    }

printf("\n\n Product of the two matrix is:\n");
for(i=0;i<r1;i++)
    {
    printf("\n");
    for(j=0;j<c2;j++)
        {
        mat3[i][j]=0;
        printf("\t");
        for(k=0;k<c1;k++)
            {
            mat3[i][j]+=mat1[i][k]*mat2[k][j];
            }
            printf("%d",mat3[i][j]);
        }
    }
getch();
}

No comments:

Post a Comment

If you have any doubt, feel free to ask...