#include
#include
#include
float detrm(float[25][25],float);
void cofact(float[25][25],float);
void trans(float[25][25],float[25][25],float);
void main()
{
float a[25][25],d;
int i,j,n;
clrscr();
printf("Enter the order of the matrix:\n");
scanf("%d",&n);
printf("Enter the %d elements of the matrix:\n",n);
for(i=0;i
for(j=0;j
scanf("%f",&a[i][j]);
}
}
d=detrm(a,n);
printf("\nThe determinant is %f",d);
if(d==0)
printf("\nMatrix is not inversible\n");
else
cofact(a,n);
getch();
}
float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
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 cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
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]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}
void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i
for(j=0;j
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i
for(j=0;j
inv[i][j]=b[i][j]/d;
}
}
printf("\n\nThe inverse of the matrix is :\n");
for(i=0;i
printf("\n");
for(j=0;j
printf("%f ",inv[i][j]);
}
}
}
Output
-------------------------------------------
Enter the order of the matrix:
2
Enter the 2 elements of the matrix:
1
2
3
4
The determinant is -2.000000
The inverse of the matrix is :
-2.000000 1.000000
1.500000 -0.500000
No comments:
Post a Comment