Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Write a C Program to Multiply Two Matrices


Source Code :

#include<stdio.h>
void main()
{
      int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
      clrscr();
      printf("\nEnter Row & Column of Matrix A : ");
      scanf("%d %d",&m,&n);   //Input of row & column of Matrix A
      printf("\nEnter Row & Column of Matrix B : ");
      scanf("%d %d",&p,&q);   //Input of row & column of Matrix B
      if(n==p)    //If-else checking whether Matrix can be multiplied
      {
            printf("\nMatrix can be Multiplied\n");
            printf("\nInput Matrix A : ");
            for(i=0;i<m;i++)
            {
                  for(j=0;j<n;j++)
                  {
                        scanf("%d",&a[i][j]);   //Input in Matrix A
                  }
            }

            printf("\nInput Matrix B : ");
            for(i=0;i<p;i++)
            {
                  for(j=0;j<q;j++)
                  {
                        scanf("%d",&b[i][j]);   //Input in Matrix B
                  }
            }
            printf("\nMatrix A : \n");
            for(i=0;i<m;i++)
            {
                  printf("\n");
                  for(j=0;j<n;j++)
                  {
                        printf("%d\t",a[i][j]); //Display Matrix A
                  }
            }
            printf("\nMatrix B : \n");
            for(i=0;i<p;i++)
            {
                  printf("\n");
                  for(j=0;j<q;j++)
                  {
                        printf("%d\t",b[i][j]); //Display Matrix B
                  }
            }
            for(i=0;i<q;i++)
            {
                  for(j=0;j<n;j++)
                  {
                        {
                             c[i][j]=a[i][j]*b[j][i]; //Multiplication
                        }
                  }
            }
            printf("\nMultiplication of Matrix A & B : \n");
            for(i=0;i<q;i++)
            {
                  printf("\n");
                  for(j=0;j<n;j++)
                  {
                      printf("%d\t",c[i][j]); //Display Resultant Matrix
                  }
            }
      }
      else
      {
            printf("\nMatrix cannot be multiplied");
      }
      printf(“\nPress any Key to Continue………”);
      getch();
}

Output :

Enter Row & Column of Matrix A : 2   3

Enter Row & Column of Matrix B : 3   2

Matrix can be Multiplied

Input Matrix A : 1  2  3  4  5  6

Input Matrix B : 6  5  4  3  2  1

Matrix A :
1     2     3
4     5     6

Matrix B :
6     5
4     3
2     1

Multiplication of Matrix A & B :
6     8     6
20    15    6

Press any Key to Continue…………………

SAMPLE DATA 2 :

Enter Row & Column of Matrix A : 3   3

Enter Row & Column of Matrix B : 3   2

Matrix cannot be Multiplied

Press any Key to Continue……………

No comments:

Post a Comment