//program to find the inverse of a matrix
#include< conio.h >
#include< stdio.h >
#include< math.h >
void trans(int a[10][10],int fac[10][10],int k,float dt)
{
int i,j;
float r[10][10];
printf("\n\nThe inverse of the matrix is :\n\n");
for(i=0;i
{
for(j=0;j
{
r[i][j]=(fac[j][i]/dt);
printf("%f\t",r[i][j]);
}
printf("\n\n");
}
}
float detrm(int a[10][10],int k)
{
int s=1,b[10][10];
float det;
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c
{
m=0;
n=0;
for(i=0;i
{
for(j=0;j
{
b[i][j]=0;
if(i!=0 && j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}
void cofac(int a[10][10],int k,float dt)
{
int b[10][10],fac[10][10];
int i,j,p,q,c,d,m,n;
for(q=0;q
{
for(p=0;p
{
m=0;
n=0;
for(i=0;i
{
for(j=0;j
{
b[i][j]=0;
if(i!=q && j!=p)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,k-1);
}
}
trans(a,fac,k,dt);
}
void main()
{
int a[10][10],m,n,i,j;
float d;
clrscr();
printf("\n\n\t\t***MATRIX INVERSE***\n\n");
printf("Enter the 3/3 matrix :\n");
m=3;n=3;
for(i=0;i
{
for(j=0;j
scanf("%d",&a[i][j]);
}
printf("\n\nThe matrix is: \n\n");
for(i=0;i
{
for(j=0;j
{
printf("%d ",a[i][j]);
}
printf("\n");
}
i=0;
printf("\n\tThe determinant of the matrix is:= ");
printf("%f",d=detrm(a,n));
if(d!=0)
cofac(a,n,d);
else
printf("\n\n\t\t No inverse exists ....");
getch();
}
No comments:
Post a Comment