Wednesday, July 6, 2011

Program to find the product of two Matrices


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int m1[10][10];
  int m2[10][10];
  int p[10][10];
  int i,j,k,r,c;
  for(i=0;i<10;i++)
  {
      for(j=0;j<10;j++)
      {
      m1[i][j]=0;
      m2[i][j]=0;
      p[i][j]=0;
      }
  }
  cout<<"Enter size of row and colomn:"<<endl;
  cin>>r>>c;
  cout<<" Enter the two matrices: "<<endl;
  for(i=0;i<r;i++)
  {
      for(j=0;j<c;j++)
      {
    cout<<"Enter element of m1: "<<(i+1)<<(j+1)<<endl;
    cin>>m1[i][j];
    cout<<"Enter elementof m2:"<<(j+1)<<(i+1)<<endl;
    cin>>m2[j][i];
      }
  }
  cout<<" The first matrix is: "<<endl;
  for(i=0;i<r;i++)
  {
      for(j=0;j<c;j++)
      {
     cout<<m1[i][j];
      }
      cout<<"\n";
  }
  cout<<"The second matrix is: "<<endl;
  for(i=0;i<c;i++)
  {
      for(j=0;j<r;j++)
      {
     cout<<m2[i][j];
      }
      cout<<"\n";
  }
  cout<<"Product of matrices is:"<<endl;
  for(i=0;i<r;i++)
  {
      for(j=0;j<r;j++)
      {
     for(k=0;k<c;k++)
     {
      p[i][j]+=m1[i][k]*m2[k][j];
     }
     cout<<p[i][j];
      }
      cout<<"\n";
  }
  getch();
}

No comments:

Post a Comment