C Program to Find Inverse of Matrix of any order

C Program to Find Inverse of Matrix of any given order

Inverse of Matrix for a matrix A is A-1.

The inverse of A is A-1 only when: A × A-1 = A-1 × A = I

We can find the Inverse of the matrix with the help of adjoin matrix that is :-

Inverse of a matrix

C-Program to read any order of Square Matrix and find its inverse.

/* C-Program: Inverse Matrix of any Order of Square Matrix */

#include<stdio.h>
#include<conio.h>

/* Function  Prototype declaration */
float detr(float [][10],int);
void cofactor(float [][10],int);
void transpose(float [][10],float [][10],int);
void display(float [][10], int);

int main()
{
  float a[10][10],d;
  int i,j,k;

  printf("Enter the order of the Matrix : ");
  scanf("%d", &k);
  printf("Enter the row by row values of %dX%d Matrix:\n",k,k);
  for (i = 0;i < k; i++)
    for (j = 0;j < k; j++)
       scanf("%f", &a[i][j]);
  printf("The given matrix is:\n");
  display(a, k);
  d=detr(a,k);
  printf("The determinant of matrix is: %.2f \n",d);
  if(d==0)
    printf("Inverse of Matrix is not possible\n");
  else
    cofactor(a,k);
    
    return 0;
}

/* for calculate Deterrminant of the Matrix function definition*/
float detr(float a1[10][10],int k)
{
    float d=0,b[10][10];
    int i,j,m,n,c,s=1;
    if(k==1)
        return (a1[0][0]);
    for(c=0;c<k;c++)
    {
        m=0;  n=0;
        for(i=0;i<k;i++)
          for(j=0;j<k;j++)
          {
             if(i!=0 && j!=c)
             {
                b[m][n]=a1[i][j];
                n++;
                if(n == k-1)
                {
                    n=0; m++;
                }
             }
          }
        d=d+s*(a1[0]*detr(b,k-1));
        s = -s;
    }
    return(d);
}
/* function define find co-factor */
void cofactor(float x[10][10],int k)
{
 float b[10][10], y[10][10];
 int p,q,m,n,i,j,s=1;
 for(q=0;q<k;q++)
    for(p=0;p<k;p++)
    {
        m=0; n=0;
        for(i=0;i<k;i++)
            for(j=0;j<k;j++)
                if(i!=q && j!=p)
                {
                    b[m][n]=x[i][j];
                    if(n<(k-2))
                        n++;
                    else
                    {
                        n=0; m++;
                    }
                }
        y[q][p]=s*detr(b,k-1);
        s=-1*s;
    }
    transpose(x,y,k);
}

/*Finding transpose of matrix*/
void transpose(float x[10][10],float y[10][10],int r)
{
    int i,j;
    float b[10][10],inv[10][10],d;
    printf("The cofactor of matrix is:\n");
    display(y,r);
    /* transpose of cofactor matrix */
    for(i=0;i<r;i++)
        for(j=0;j<r;j++)
            b[i][j]=y[j][i];

    d=detr(x,r);
    printf("The adjoint matrix is:\n");
    display(b,r);
    /* calculate inverse matrix */
    for(i=0;i<r;i++)
        for(j=0;j<r;j++)
            inv[i][j]=b[i][j]/d;

    printf("The inverse of matrix is:\n");
    display(inv,r);
}
/* display function definition */
void display(float m[][10], int o)
{    int i,j;
    for(i=0;i<o;i++)
    {
        for(j=0;j<o;j++)
            printf("%8.2f", m[i][j]);
        printf("\n");
    }
}

OUTPUT :-

EXAMPLE 1

Enter the order of the Matrix : 3
Enter the row by row values of 3X3 Matrix:
0 1 3
1 2 1
2 3 1
The given matrix is:
    0.00    1.00    3.00
    1.00    2.00    1.00
    2.00    3.00    1.00
The determinant of matrix is: -2.00
The cofactor of matrix is:
   -1.00    1.00   -1.00
    8.00   -6.00    2.00
   -5.00    3.00   -1.00
The adjoint matrix is:
   -1.00    8.00   -5.00
    1.00   -6.00    3.00
   -1.00    2.00   -1.00
The inverse of matrix is:
    0.50   -4.00    2.50
   -0.50    3.00   -1.50
    0.50   -1.00    0.50

EXAMPLE 2

Enter the order of the Matrix : 3
Enter the row by row values of 3X3 Matrix:
1 2 3
1 2 3
2 3 4
The given matrix is:
    1.00    2.00    3.00
    1.00    2.00    3.00
    2.00    3.00    4.00
The determinant of matrix is: 0.00
Inverse of Matrix is not possible

EXAMPLE 3

Enter the order of the Matrix : 4
Enter the row by row values of 4X4 Matrix:
1 1 1 1
0 2 1 3
4 2 1 0
3 2 1 5
The given matrix is:
    1.00    1.00    1.00    1.00
    0.00    2.00    1.00    3.00
    4.00    2.00    1.00    0.00
    3.00    2.00    1.00    5.00
The determinant of matrix is: -17.00
The cofactor of matrix is:
    0.00   17.00  -34.00   -0.00
   -5.00   14.00   -8.00   -1.00
   -2.00   -8.00    7.00    3.00
    3.00   -5.00   -2.00    4.00
The adjoint matrix is:
    0.00   -5.00   -2.00    3.00
   17.00   14.00   -8.00   -5.00
  -34.00   -8.00    7.00   -2.00
   -0.00   -1.00    3.00    4.00
The inverse of matrix is:
   -0.00    0.29    0.12   -0.18
   -1.00   -0.82    0.47    0.29
    2.00    0.47   -0.41    0.12
    0.00    0.06   -0.18   -0.24

Leave a Reply