0% found this document useful (0 votes)
296 views3 pages

Matrix Inversion Program

This C program finds the inverse of a non-singular square matrix. It takes a matrix as input, calculates its determinant, and if non-zero, calculates the inverse by finding the matrix of cofactors, taking the transpose, and dividing by the determinant. The inverse matrix is then printed as output. Key steps include using recursion to calculate the determinant of matrices, finding the cofactors of matrices by removing rows and columns, and taking the transpose of the matrix of cofactors before dividing by the determinant.

Uploaded by

Vinay Dhingra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views3 pages

Matrix Inversion Program

This C program finds the inverse of a non-singular square matrix. It takes a matrix as input, calculates its determinant, and if non-zero, calculates the inverse by finding the matrix of cofactors, taking the transpose, and dividing by the determinant. The inverse matrix is then printed as output. Key steps include using recursion to calculate the determinant of matrices, finding the cofactors of matrices by removing rows and columns, and taking the transpose of the matrix of cofactors before dividing by the determinant.

Uploaded by

Vinay Dhingra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

PROGRAM 12

TO FIND THE INVERSE OF A GIVE NON-SINGULAR SQUARE MATRIX

#include<stdio.h>
#include<conio.h>
#include<math.h>
float determinant(float[][25],float);
void cofactor(float[][25],float);
void transpose(float[][25],float[][25],float);
int main()
{
float a[25][25],k,d;
int i,j;
printf(" enter the order of the matrix");
scanf("%f",&k);
printf(" enter the elements of matrix");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=determinant(a,k);
if(d==0)
printf("\n inverse of matrix does not exist");
else
cofactor(a,k);
}
float determinant(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<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;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]*determinant(b,k-1));
s=-1*s;
}
}
return(det);
}
void cofactor(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
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)*determinant(b,f-1);
}
}
transpose(num,fac,f);
}
void transpose(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inverse[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
}
inverse[i][j]=b[i][j]/d;
}
}
printf("\n\n\nThe inverse of matrix is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inverse[i][j]);
}
printf("\n")
}
}

You might also like