Professional Documents
Culture Documents
Matrix Reverse
Matrix Reverse
h>
int main()
{
float A[3][3];// the matrix that is entered by user
float B[3][3];//the transpose of a matrix A
float C[3][3];//the adjunct matrix of transpose of a matrix A not adjunct of
A
double X[3][3];//the inverse
int i,j;
float x,n=0;//n is the determinant of A
for(i=0,j=0;j<3;j++)
{
if(j==2)
n+=A[i][j]*A[i+1][0]*A[i+2][1];
else if(j==1)
n+=A[i][j]*A[i+1][j+1]*A[i+2][0];
else
n+=A[i][j]*A[i+1][j+1]*A[i+2][j+2];
}
for(i=2,j=0;j<3;j++)
{
if(j==2)
n-=A[i][j]*A[i-1][0]*A[i-2][1];
else if(j==1)
n-=A[i][j]*A[i-1][j+1]*A[i-2][0];
else
n-=A[i][j]*A[i-1][j+1]*A[i-2][j+2];
}
if(n!=0) x=1.0/n;
else
{
cout<<"Division by 0, not good!\n";
cout<<"=================================================================
====\n"<<endl;
return 0;
}
cout<<"\n========== The transpose of a matrix A
==============================\n";
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
{
B[i][j]=A[j][i];
cout<<" B["<<i<<"]["<<j<<"]= "<<B[i][j];
}
}
cout<<endl<<endl;
C[0][0]=B[1][1]*B[2][2]-(B[2][1]*B[1][2]);
C[0][1]=(-1)*(B[1][0]*B[2][2]-(B[2][0]*B[1][2]));
C[0][2]=B[1][0]*B[2][1]-(B[2][0]*B[1][1]);
C[1][0]=(-1)*(B[0][1]*B[2][2]-B[2][1]*B[0][2]);
C[1][1]=B[0][0]*B[2][2]-B[2][0]*B[0][2];
C[1][2]=(-1)*(B[0][0]*B[2][1]-B[2][0]*B[0][1]);
C[2][0]=B[0][1]*B[1][2]-B[1][1]*B[0][2];
C[2][1]=(-1)*(B[0][0]*B[1][2]-B[1][0]*B[0][2]);
C[2][2]=B[0][0]*B[1][1]-B[1][0]*B[0][1];
}
}
cout<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
X[i][j]=C[i][j]*x;
}
}
}
}
cout<<endl<<endl;
return 0;
}