Inverse of a Matrix in C

Program to find inverse of a matrix.

#include<stdio.h>
#include<math.h>
float detrm( float[][], float );
void cofact( float[][], float );
void trans( float[][], float[][], float );
main()
{
    float a[ 25 ][ 25 ], k, d;
    int i, j;
    printf( "ENTER THE ORDER OF THE MATRIX:\n" );
    scanf( "%f", &k );
    printf( "ENTER THE ELEMENTS OF THE MATRIX:\n" );
 
    for ( i = 0;i < k;i++ )
        {
            for ( j = 0;j < k;j++ )
                {
                    scanf( "%f", &a[ i ][ j ] );
                }
        }
 
    d = detrm( a, k );
    printf( "THE DETERMINANT IS=%f", d );
 
    if ( d == 0 )
        printf( "\nMATRIX IS NOT INVERSIBLE\n" );
    else
        cofact( a, k );
}
 
/******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/
 
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 < 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 ] * detrm( b, k – 1 ) );
                    s = -1 * s;
                }
        }
 
    return ( det );
}
 
/*******************FUNCTION TO FIND COFACTOR*********************************/
 
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 < f;q++ )
        {
            for ( p = 0;p < f;p++ )
                {
                    m = 0;
                    n = 0;
 
                    for ( i = 0;i < f;i++ )
                        {
                            for ( j = 0;j < f;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 );
}
 
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
 
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 < r;i++ )
        {
            for ( j = 0;j < r;j++ )
                {
                    b[ i ][ j ] = fac[ j ][ i ];
                }
        }
 
    d = detrm( num, r );
    inv[ i ][ j ] = 0;
 
    for ( i = 0;i < r;i++ )
        {
            for ( j = 0;j < r;j++ )
                {
                    inv[ i ][ j ] = b[ i ][ j ] / d;
                }
        }
 
    printf( "\nTHE INVERSE OF THE MATRIX:\n" );
 
    for ( i = 0;i < r;i++ )
        {
            for ( j = 0;j < r;j++ )
                {
                    printf( "\t%f", inv[ i ][ j ] );
                }
 
            printf( "\n" );
        }
}
VN:F [1.9.22_1171]
Rating: 7.5/10 (125 votes cast)
VN:F [1.9.22_1171]
Rating: +30 (from 56 votes)
Inverse of a Matrix in C, 7.5 out of 10 based on 125 ratings

01. December 2008 by Jishnu
Categories: C Programming | Tags: | 23 comments

Comments (23)

  1. it’s easy but very lengthy

  2. thanks a lot
    a simple code but very effective!!!!!!!!!!

  3. great work dude ,thanks now i dont have to worry about practicals

  4. the approach is good…but it will much better if you implement guassian elemination method

  5. hi guys, Program is really good. Does the inverse work for matrix of any dimensions. Quick reply would be appreciated. Thanks in advance

  6. it will allow upto dimension of 25×25 chage size of array for higher matrixes.

  7. I am surprised to see how simple this code is!!

  8. Hey what a GREAT PROGRAM
    I never expected a much simpler program like this!!!!!

  9. Jst the right think I needed…..thaaanx

  10. Pingback: Matrix Inverse Code as Module using Pointers | The Code Cracker

  11. really mind blowing………………….

  12. easy long program

  13. There is bug
    There is no need for initialize inv
    inv[ i ][ j ] = 0; where i ans j value is equal to r and if you give r = 25 it will corrupt the stack.

  14. I got this error:

    error C2371: ‘trans’ : redefinition; different basic types

    at line:

    –>{
    int i, j;
    float b[ 25 ][ 25 ], inv[ 25 ][ 25 ], d;

    Anyone have any idea why? Please help me as I couldn’t figure out what’s the problem.
    Thanks.

  15. nice work …. but u could have did it more easily by using pointers.

    Another code using pointers is at :
    http://chi3x10.wordpress.com/2008/05/28/calculate-matrix-inversion-in-c/

  16. thank you for sharing this code. I try to get invers for a matrix

  17. thank you forwritten proper pattern sharing this code. I try to get invers for a matrix

  18. we can also write this programme as

    #include
    #include
    int sdet(int [][100],int,int,int);
    int det(int a[][100],int n)
    {
    int i=0,s=0,t=1;
    if(n==1)
    return(a[0][i]);
    else
    {
    for(i=0;i<n;i++)
    {
    s=s+a[0][i]*sdet(a,n,0,i)*t;
    t=t*-1;
    }
    return(s);
    }
    }
    int sdet(int a[][100],int n,int x,int y)
    {
    int b[100][100],i,c,j,d,k=1,l=1;
    for(i=0,c=0;i<n;i++,c++)
    {
    if(i==x)
    c–;
    else
    {
    for(j=0,d=0;j<n;j++,d++)
    {
    if(j==y)
    d–;
    else
    b[d]=a[i][j];
    }
    }}
    return(det(b,n-1));
    }
    void inv(int a[][100],int n)
    {
    int i,j,t=1,d;
    float c[100][100];
    d=det(a,n);
    cout<<endl<<"inverse of matrix 'a' is….."<<endl<<endl;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    if((i+j)%2==0)
    t=1;
    else
    t=-1;
    c[i][j]=(1.0/d)*sdet(a,n,i,j)*t;
    cout<<c[i][j]<<" ";
    }
    cout<<endl;
    }
    }
    int main()
    {
    int a[100][100],i,j,n;
    cout<>n;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    cout<<"enter the value of element a["<<i<<"]["<<j<>a[i][j];
    }
    }
    cout<<endl<<"determinant of matrix 'a' is : "<<det(a,n)<<endl;
    if(det(a,n)!=0)
    inv(a,n);
    else
    cout<<"inverse is not possible";
    return(0);
    }

  19. also try this programme

    #include
    #include
    int sdet(int [][100],int,int,int);
    int det(int a[][100],int n)
    {
    int i=0,s=0,t=1;
    if(n==1)
    return(a[0][i]);
    else
    {
    for(i=0;i<n;i++)
    {
    s=s+a[0][i]*sdet(a,n,0,i)*t;
    t=t*-1;
    }
    return(s);
    }
    }
    int sdet(int a[][100],int n,int x,int y)
    {
    int b[100][100],i,c,j,d,k=1,l=1;
    for(i=0,c=0;i<n;i++,c++)
    {
    if(i==x)
    c–;
    else
    {
    for(j=0,d=0;j<n;j++,d++)
    {
    if(j==y)
    d–;
    else
    b[d]=a[i][j];
    }
    }}
    return(det(b,n-1));
    }
    void inv(int a[][100],int n)
    {
    int i,j,t=1,d;
    float c[100][100];
    d=det(a,n);
    cout<<endl<<"inverse of matrix 'a' is….."<<endl<<endl;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    if((i+j)%2==0)
    t=1;
    else
    t=-1;
    c[i][j]=sdet(a,n,i,j)*t;
    cout<<c[i][j]<<"/"<<d<<" ";
    }
    cout<<endl;
    }
    cout<<endl<<"in simplified form it is….."<<endl<<endl;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    cout<<(1.0/d)*c[i][j]<<" ";
    cout<<endl;
    }
    }
    int main()
    {
    int a[100][100],i,j,n;
    cout<>n;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    cout<<"enter the value of element a["<<i<<"]["<<j<>a[i][j];
    }
    }
    cout<<endl<<"determinant of matrix 'a' is : "<<det(a,n)<<endl;
    if(det(a,n)!=0)
    inv(a,n);
    else
    cout<<"inverse is not possible";
    return(0);
    }

  20. Hi,

    I think this program is wrong for some instances, as the matrix
    1 0 1
    0 1 0
    0 2 3
    Its inverse should be
    1 2/3 -1/3
    0 1 0
    0 -2/3 1/3
    But this program the result is
    1 0 0
    0 1 0
    0 0 0

  21. Whts d need for b[i][j]=0 in d function to find determinant?

  22. Stupid programme in the world

Leave a Reply