Monday, June 18, 2012

To find the product of two matrices


/* Program to find the product of two matrixes */

#include<stdio.h>

#include<conio.h>

void main()

{

  int a[100][100],b[100][100],c[100][100];

  int i,j,k,r1,c1,r2,c2;

  clrscr();

  printf("Enter the order of Matrix A:\n");

  scanf("%d%d",&r1,&c1);

  printf("Enter the order of Matrix B:\n");

  scanf("%d%d",&r2,&c2);

  /* Condition checking */

  if(c1==r2)

  {

    printf("\nEnter the elements of Matrix:A-->\n");

    for(i=0;i<r1;i++)

    {

      for(j=0;j<c1;j++)

      {

            scanf("%d",&a[i][j]);

      }

    }

    printf("\nEnter the elements of Matrix:B-->\n");

    for(i=0;i<r2;i++)

    {

      for(j=0;j<c2;j++)

      {

            scanf("%d",&b[i][j]);

      }

    }

    for(i=0;i<r1;i++)

    {

      for(j=0;j<c2;j++)

      {

            c[i][j]=0;

            for(k=0;k<r2;k++)

            {

              c[i][j]=c[i][j]+a[i][k]*b[k][j];

            }

      }

    }

    printf("\n\nProduct of two matrix:\n");

    for(i=0;i<r1;i++)

    {

      for(j=0;j<c2;j++)

      {

            printf("\t%d",c[i][j]);

      }

      printf("\n");

    }

  }

  else

    printf("\nMultiplication is not possible!");

  getch();

}

 

OUTPUT

Enter the order of Matrix A:

3

2

Enter the order of Matrix B:

2

3

 

Enter the elements of Matrix:A-->

2

3

4

1

0

3

 

Enter the elements of Matrix:B-->

1

4

3

2

0

0

 

 

Product of two matrix:

        8       8       6

        6       16      12

        6       0       0

 

No comments:

Post a Comment

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