Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,146 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,350 people online right now. Registration is fast and FREE... Join Now!





Inverse Matrix

Finds the determinant of 3x3 matrix, then the transpose, then the adjunct of the transpose, then the inverse

Submitted By: PennyBoki
Actions:
Rating:
Views: 7,714

Language: C++

Last Modified: August 5, 2007
Instructions: Copy/Paste/Compile/Run

Snippet


  1. //PennyBoki @ </dream.in.code>
  2. #include <iostream.h>
  3.  
  4. int main()
  5. {
  6.      float A[3][3];// the matrix that is entered by user
  7.      float B[3][3];//the transpose of a matrix A
  8.      float C[3][3];//the adjunct matrix of transpose of a matrix A not adjunct of A
  9.      double X[3][3];//the inverse
  10.      int i,j;
  11.      float x,n=0;//n is the determinant of A
  12.      
  13.      cout<<"========== Enter matrix A =============================================\n";
  14.      for(i=0;i<3;i++)
  15.      {     cout<<endl;
  16.           for(j=0;j<3;j++)
  17.           {     
  18.                cout<<" A["<<i<<"]["<<j<<"]= ";
  19.                cin>>A[i][j];
  20.                B[i][j]=0;
  21.                C[i][j]=0;
  22.           }
  23.      }
  24.      
  25.      
  26.           for(i=0,j=0;j<3;j++)
  27.           {     
  28.                if(j==2)
  29.                n+=A[i][j]*A[i+1][0]*A[i+2][1];
  30.                else if(j==1)
  31.                n+=A[i][j]*A[i+1][j+1]*A[i+2][0];
  32.                else
  33.                n+=A[i][j]*A[i+1][j+1]*A[i+2][j+2];
  34.           }
  35.           for(i=2,j=0;j<3;j++)
  36.           {     
  37.                if(j==2)
  38.                n-=A[i][j]*A[i-1][0]*A[i-2][1];
  39.                else if(j==1)
  40.                n-=A[i][j]*A[i-1][j+1]*A[i-2][0];
  41.                else
  42.                n-=A[i][j]*A[i-1][j+1]*A[i-2][j+2];
  43.           }
  44.  
  45.  
  46.      cout<<"\n========== The matrix A is ==========================================\n";     
  47.      for(i=0;i<3;i++)
  48.      {
  49.           cout<<endl;
  50.           for(j=0;j<3;j++)
  51.           {     
  52.                cout<<" A["<<i<<"]["<<j<<"]= "<<A[i][j];
  53.           }
  54.      }
  55.      cout<<endl<<endl;
  56.      
  57.      cout<<"=====================================================================\n"<<endl;
  58.      cout<<"The determinant of matrix A is "<<n<<endl<<endl;
  59.      cout<<"====================================================================="<<endl;
  60.      
  61.      if(n!=0) x=1.0/n;
  62.      else
  63.      {
  64.           cout<<"Division by 0, not good!\n";
  65.           cout<<"=====================================================================\n"<<endl;
  66.           return 0;
  67.      }
  68.      cout<<"\n========== The transpose of a matrix A ==============================\n";     
  69.      for(i=0;i<3;i++)
  70.      {
  71.           cout<<endl;
  72.           for(j=0;j<3;j++)
  73.           {     
  74.                    
  75.                B[i][j]=A[j][i];
  76.                cout<<" B["<<i<<"]["<<j<<"]= "<<B[i][j];
  77.                
  78.           }
  79.      }
  80.      cout<<endl<<endl;
  81.  
  82.  
  83.      C[0][0]=B[1][1]*B[2][2]-(B[2][1]*B[1][2]);
  84.      C[0][1]=(-1)*(B[1][0]*B[2][2]-(B[2][0]*B[1][2]));
  85.      C[0][2]=B[1][0]*B[2][1]-(B[2][0]*B[1][1]);
  86.      
  87.      C[1][0]=(-1)*(B[0][1]*B[2][2]-B[2][1]*B[0][2]);
  88.      C[1][1]=B[0][0]*B[2][2]-B[2][0]*B[0][2];
  89.      C[1][2]=(-1)*(B[0][0]*B[2][1]-B[2][0]*B[0][1]);
  90.  
  91.      C[2][0]=B[0][1]*B[1][2]-B[1][1]*B[0][2];
  92.      C[2][1]=(-1)*(B[0][0]*B[1][2]-B[1][0]*B[0][2]);
  93.      C[2][2]=B[0][0]*B[1][1]-B[1][0]*B[0][1];
  94.  
  95.  
  96.      cout<<"\n========== The adjunct matrix of transpose of the matrix A ==========\n";     
  97.      for(i=0;i<3;i++)
  98.      {
  99.           cout<<endl;
  100.           for(j=0;j<3;j++)
  101.           {     
  102.                cout<<" C["<<i<<"]["<<j<<"]= "<<C[i][j];
  103.                
  104.           }
  105.      }
  106.      cout<<endl<<endl;
  107.  
  108.      
  109.      for(i=0;i<3;i++)
  110.      {
  111.           for(j=0;j<3;j++)
  112.           {     
  113.                X[i][j]=C[i][j]*x;
  114.                
  115.           }
  116.      }
  117.  
  118.  
  119.  
  120.      cout<<"\n========== The inverse matrix of the matrix you entered!!! ==========\n";
  121.      for(i=0;i<3;i++)
  122.      {     cout<<endl;
  123.           for(j=0;j<3;j++)
  124.           {     
  125.                cout<<" X["<<i<<"]["<<j<<"]= "<<X[i][j];
  126.          
  127.           }
  128.      }
  129.      cout<<endl<<endl;
  130.      
  131.  
  132. return 0;
  133. } 

Copy & Paste


Comments


rj_1988524 2008-03-16 00:26:35

nice 1


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month