Wednesday, 10 February 2016

Matrix-Multiplication using class(C++)


/* Program that take any two matrices from user as input and show their matrix-multiplication as the result , implemented using class*/

input form of order of matrices from user
input form of matrices elements
output form of final matrix
 #include <iostream>

  using namespace std;
  
  class MATRIX
  {
 
  private :
  int M1[20][20];   //member variable to store matrix 1
  int M2[20][20];   //member variable to store matrix 2
  int M_FINAL[20][20];   //data member to store the final matrix

     public :
  int R1 , C1;
  int R2 , C2;
  int M_MULTI_POSSIBILITY( void );  //M.F 1
  void MATRICES_INPUT( void );      //M.F 2
  void M_MULTIPLICATION( void );    //M.F 3
  void DISPLAY( void );             //M.F 4
 
  };


 //memeber function to check for possibility of matrix- 
 //multiplication
  int MATRIX :: M_MULTI_POSSIBILITY( void )
  {
  int flag = 0;
 
  if( C1 == R2 ) {
  cout<<" Multiplication of given order of matrix is !! POSSIBLE !! ";
      return (++flag);
        }
     
  else {
        cout<<" Multiplication ! NOT POSSIBLE ! with given order of matrix .\n\n";
        return flag;
}
 }
  
  //member function to input-
  //matrices if multiplication condition is -
  //satisfied
  void MATRIX :: MATRICES_INPUT( void )
  {
 
  system("cls");
  int j , k ;
 
       cout<<"Enter elements of matrix 1 >>";
for( j = 0 ; j < R1 ; j++ ) {
          cout<<"\n\n\tRow"<<j+1<<" ->\n";
            for( k = 0 ; k < C1 ; k++ ) {
              cout<<"\tColumn"<<k+1<<" :>\t";
                cin>>M1[j][k];
            } 
       }
        
      cout<<"\nEnter elements of matrix 2 >>";
      for( j = 0 ; j < R2 ; j++ ) {
          cout<<"\n\n\tRow"<<j+1<<" ->\n";
         for( k = 0 ; k < C2 ; k++ ) {
           cout<<"\tColumn"<<k+1<<" :>\t";
             cin>>M2[j][k];
         }
     }
  
 }
  
  //member function to perform multiplication
  void MATRIX :: M_MULTIPLICATION( void )
  {
  int i , j , k ;
   for( i = 0 ; i < R1 ; i++ )
       for( j = 0 ; j < C2 ; j++ )
M_FINAL[i][j]=0;

  for( i = 0 ; i < R1 ; i++ )
     for( j = 0 ; j < C2 ; j++ )
         for( k = 0 ; k < C1 ; k++ )
          M_FINAL[i][j] += M1[i][k] * M2[k][j] ;
     
  }
  
  
  //member function to display final matrix
  void MATRIX :: DISPLAY( void )
  {
 
  system("cls");
       int i , j ;
 
  cout<<"c@vijay\n\n\a\a\tFinal matrix after multiplication is :\n\n\t";
       for( j = 0 ; j < C2 ; j++ )
           cout<<"\tC"<<j+1;
      for( i = 0 ; i < R1 ; i++ ) {
           cout<<"\n\tR"<<i+1<<"\t";
         for( j = 0 ; j < C2 ; j++ )
              cout<<M_FINAL[i][j]<<"\t";
    
         cout<<"\n\t\t";
      }
 
  }
  
  int main( void )
  {
 
  MATRIX obj;  //object creation of MATRIX class



 
       int choice = 0;
 
  do {
 
  system("cls");
 
     cout<<"c@vijay\t\t\tMATRIX MULTIPLICATION ";
  cout<<"\n\nEnter the order of matrix 1 :\n\t no. of rows > ";
  cin>>obj.R1;
  cout<<"\n\t no. of columns > ";
  cin>>obj.C1;
  cout<<"\n\nEnter the order of matrix 2 :\n\t no. of rows > ";
  cin>>obj.R2;
  cout<<"\n\t no. of columns > ";
  cin>>obj.C2;
 
  system("cls");
  if(obj.M_MULTI_POSSIBILITY()) //calling M.F 1
           obj.MATRICES_INPUT();     //calling M.F 2
     
  else
      goto menu;
 
          obj.M_MULTIPLICATION();   //calling M.F 3
      obj.DISPLAY();    // calling M.F 4
 
  menu :
  cout<<"\n\n XD Do you want to enter values again for another matrices \n\n\tPRESS 1 to 'continue' 0 to 'abort' > ";
  cin>>choice;
   
      } while( choice == 1 );
     

return 0;

  }

No comments:

Post a Comment