C Program to Find Inverse of a Matrix

Matrix Inverse

C Program to Find Inverse of a Matrix

In this blog, we will write a C program to find inverse of a matrix.

Let's write the program

Program

        
#include<stdio.h>
#include<math.h>
                
//function prototype that are being created
void cofactor(float [][25], float);
float determinant(float [][25], float);
void transpose(float [][25], float [][25], float);

int main()
{
    float a[25][25], n, d;
    int i, j;
                
    printf("Enter the order of the Matrix: ");
    scanf("%f", &n);
                
    printf("Enter the elements of a matrix: \n");

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%f", &a[i][j]);
        }
    }
                
    d = determinant(a, n);
    if (d == 0)
    printf("Since the determinant is zero (0), therefore inverse is not possible.");
    else
    cofactor(a, n);
}

                
// function for the calculation of determinant

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);
}
                
                
// function for cofactor calculation

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);
}
                
                
//function to find the transpose of a matrix

 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("\nThe inverse of matrix: \n");

    for (i = 0; i < r; i++)
    {
        for (j = 0; j < r; j++)
        {
            printf("\t%f", inverse[i][j]);
        }
        printf("\n");
    }
}                   
        
    

Output 1

        
Enter the order of the Matrix: 3
Enter the elements of a matrix:
4 6 2
1 2 3
4 5 6
            
The inverse of matrix:
    -0.166667       -1.444444       0.777778
    0.333333        0.888889        -0.555556
    -0.166667       0.222222        0.111111             
        
    

Output 2

        
Enter the order of the Matrix: 3
Enter the elements of a matrix:
1 2 3
4 5 6
3 2 1
Since the determinant is zero (0), therefore inverse is not possible.             
        
    



Join Our Social Media Connections

Post a Comment

0 Comments