Hello,
I made the program to inverse a 3*3 matrix and here's the code to do that
Code:
#include <iostream.h>
int** adjoint(int **matrix,int r,int c)
{
int i,j,l,m,t,temp[4],**ad_mat;
ad_mat=new int *[r];
for(i=0;i<r;i++)
{
ad_mat[i]=new int[c];
}
for(l=0;l<r;l++)
{
for(m=0;m<c;m++)
{
t=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i!=l && j!=m)
{
temp[t]=matrix[i][j];
t++;
}
}
}
ad_mat[l][m]=(temp[0]*temp[3])-(temp[1]*temp[2]);
}
}
return ad_mat;
}
int main(void)
{
clrscr();
int **matrix,i,j,**ad_mat;
float inverse[3][3],det=0;
matrix=new int *[3];
for(i=0;i<3;i++)
{
matrix[i]=new int [3];
}
cout<<"\n\nEnter 9 Elements :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>matrix[i][j];
}
}
ad_mat=adjoint(matrix,3,3);
cout<<"\n\nAdjoint Of Matrix Is :\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout.width(4);
cout<<ad_mat[i][j];
}
cout<<endl;
}
cout<<"\n\nValues Of Matrix :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i+j==1 || i+j==3)
{
matrix[i][j]*=-1;
}
cout.width(4);
cout<<matrix[i][j];
}
cout<<endl;
}
for(j=0;j<3;j++)
{
det=det+ad_mat[0][j]*matrix[0][j];
}
if(det!=0)
{
cout<<"\n\nThe Determinant Is :"<<det;
cout<<"\n\nThe Inverse Of Matrix Is :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
inverse[i][j]=ad_mat[i][j]/det;
cout.width(8);
cout.precision(3);
cout<<inverse[i][j];
}
cout<<endl;
}
}
else
cout<<"\n\nInverse Of Matix Not Possible";
return 0;
}
Now what i want to know is that is the logic of the program correct??
This is just a 3 *3 matrix. Can a N*N matrix be inverted??If yes then kindly tell me what changes do i need to make in the above program to make it work for a N*N matrix?
i am not sure whether a N*N matrix be inverted or not??!
anybody please tell me how to find adjoint of a N*N matrix or of 4*4 matrix so that i can get a rough idea and try to implement that program in C++.