C program to find inverse of a matrix






How to find inverse of a matrix in c
C code to find inverse of a matrix
Inverse of a 3x3 matrix in c

#include<stdio.h>

int main(){

  int a[3][3],i,j;
  float determinant=0;

  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }

  for(i=0;i<3;i++)
      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

   printf("\nInverse of matrix is: \n\n");
   for(i=0;i<3;i++){
      for(j=0;j<3;j++)
           printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
       printf("\n");
   }

   return 0;
}

Enter the 9 elements of matrix: 3
5
2
1
5
8
3
9
2

The matrix is

3       5       2
1       5       8
3       9       2
Inverse of matrix is:

0.70    -0.25   0.07
-0.09   -0.00   0.14
-0.34   0.25    -0.11






Alogrithm:
**







19 comments:

Anonymous said...

i am new to programming.can anyone pls explain this to me.offer ur help as soon as possible

Unknown said...

Program above prints transpose of inverse.
see below revised code

printf("\nInverse of matrix is: \n\n");
// for(i=0;i<3;i++){
// for(j=0;j<3;j++)
for(j=0;j<3;j++){
for(i=0;i<3;i++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) -
(a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
}
printf("\n");
}

Khal said...

what about 2by2 ??

Damodhar sai said...

instead of 3 use 2 . yu need to replace dem by 2!
and also dont forget to initialise st[2]

Anonymous said...

Is program correctly work or not ?

Unknown said...

yes corectly work thanks ritesh:)

Anonymous said...

Good, but for a matrix it is also important that its determinant must be non-zero for existence of inverse.
Thus it will be good to include one more condition so it will immediately tell whether the matrix entered is valid or not.
Rest of the things are okay :)

Anonymous said...

i want 2*2 programme now! pleaseeeeee.

Anonymous said...

It does some approximations making it useless. 0.04 gets rounded to zero. 0.06 gets rounded to 0.1

Anonymous said...

if u want higher accuracy in the digits, then replace %.2f with %.10f and you will have 10 digits of accuracy instead of 2

Unknown said...

it works i tried it

Anonymous said...

HOW ABOUT 6X6 MATRIX?

General Niazi said...

why you use %3 finding determinant

Technosoft said...

write a c program to display the elements in the matrix from any starting point it should travel upside down and also want to end at starting point.
pls help me @ richardprasanna@outlook.com

Unknown said...

hello ever one

krishna said...

I can't understand why u used %3 in the code written above is it possible to write the code without %3

Unknown said...

IMPROVED PROGRAM::ONLY FOR DETERMINANTS != 0 To Find Inverse Of A Matrix


#include <studio.h>
#include <conio.h>

int i,j,k,l,m,n,p,z;
float temp,temp2,c[3][3],d[3][3],a[4],temp3[3];


void inverse()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
temp=c[i][j];
for(k=0;k<3;k++)
{
c[i][k]/=temp*(1.0);
d[i][k]/=temp*(1.0);
}
}

}
for(l=0;l<3;l++)
{
if(l!=i)
{
for(m=0;m<3;m++)
{
temp2=c[l][i];
for(n=0;n<3;n++)
{
c[l][n]=c[l][n]-(c[i][n]*temp2);
d[l][n]=d[l][n]-(d[i][n]*temp2);
}
}
}
}
}
}

void enter()
{
printf("Enter the elements of the matrix: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the ement at c[%d][%d]",i,j);
scanf("%f",&c[i][j]);
if(i==j)
{
d[i][j]=1;
}
else
{
d[i][j]=0;
}
}
}
}

void print()
{
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%f \t",d[i][j]);
}
}
}

float determinant()
{ z=0;
for(j=0;j<3;j++)
{ p=0;
for(m=1;m<3;m++)
{

for(n=0;n<3;n++)
{
if(n!=j)
{
a[p]=c[m][n];
printf("\na[%d]=%f\n",p,a[p]);
p++;
}

}
}temp3[z]=c[0][j]*(a[0]*a[3]-a[1]*a[2]);z++;
}

return(temp3[0]-temp3[1]+temp3[2]);
}

void main()
{
float g;
enter();
g=determinant();
printf("%f \n",g);
if(g!=0)
{
inverse();
print();
}
else
{
printf("Can't Be Inversed");
}
getch();
}

Unknown said...

I want to read two matrices A and B and find if A is the inverse of B... someone please help me out

Myfordbenefits said...

Very helpful article
Tnxxx a lot
http://www.networkking4u.com