C Program to Find Inverse of a Matrix

«
»
This C program sorts a given array of integer numbers using Bubble Sort technique. The algorithm gets its name from the way smaller elements “bubble” to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Time Complexity of this algorithm is O(n2).

Here is the source code of the C program to sort and display the integer array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<math.h>
  3. float determinant(float [][25], float);
  4. void cofactor(float [][25], float);
  5. void transpose(float [][25], float [][25], float);
  6. int main()
  7. {
  8.   float a[25][25], k, d;
  9.   int i, j;
  10.   printf("Enter the order of the Matrix : ");
  11.   scanf("%f", &k);
  12.   printf("Enter the elements of %.0fX%.0f Matrix : \n", k, k);
  13.   for (i = 0;i < k; i++)
  14.     {
  15.      for (j = 0;j < k; j++)
  16.        {
  17.         scanf("%f", &a[i][j]);
  18.         }
  19.     }
  20.   d = determinant(a, k);
  21.   if (d == 0)
  22.    printf("\nInverse of Entered Matrix is not possible\n");
  23.   else
  24.    cofactor(a, k);
  25. }
  26.  
  27. /*For calculating Determinant of the Matrix */
  28. float determinant(float a[25][25], float k)
  29. {
  30.   float s = 1, det = 0, b[25][25];
  31.   int i, j, m, n, c;
  32.   if (k == 1)
  33.     {
  34.      return (a[0][0]);
  35.     }
  36.   else
  37.     {
  38.      det = 0;
  39.      for (c = 0; c < k; c++)
  40.        {
  41.         m = 0;
  42.         n = 0;
  43.         for (i = 0;i < k; i++)
  44.           {
  45.             for (j = 0 ;j < k; j++)
  46.               {
  47.                 b[i][j] = 0;
  48.                 if (i != 0 && j != c)
  49.                  {
  50.                    b[m][n] = a[i][j];
  51.                    if (n < (k - 2))
  52.                     n++;
  53.                    else
  54.                     {
  55.                      n = 0;
  56.                      m++;
  57.                      }
  58.                    }
  59.                }
  60.              }
  61.           det = det + s * (a[0][c] * determinant(b, k - 1));
  62.           s = -1 * s;
  63.           }
  64.     }
  65.  
  66.     return (det);
  67. }
  68.  
  69. void cofactor(float num[25][25], float f)
  70. {
  71.  float b[25][25], fac[25][25];
  72.  int p, q, m, n, i, j;
  73.  for (q = 0;q < f; q++)
  74.  {
  75.    for (p = 0;p < f; p++)
  76.     {
  77.      m = 0;
  78.      n = 0;
  79.      for (i = 0;i < f; i++)
  80.      {
  81.        for (j = 0;j < f; j++)
  82.         {
  83.           if (i != q && j != p)
  84.           {
  85.             b[m][n] = num[i][j];
  86.             if (n < (f - 2))
  87.              n++;
  88.             else
  89.              {
  90.                n = 0;
  91.                m++;
  92.                }
  93.             }
  94.         }
  95.       }
  96.       fac[q][p] = pow(-1, q + p) * determinant(b, f - 1);
  97.     }
  98.   }
  99.   transpose(num, fac, f);
  100. }
  101. /*Finding transpose of matrix*/ 
  102. void transpose(float num[25][25], float fac[25][25], float r)
  103. {
  104.   int i, j;
  105.   float b[25][25], inverse[25][25], d;
  106.  
  107.   for (i = 0;i < r; i++)
  108.     {
  109.      for (j = 0;j < r; j++)
  110.        {
  111.          b[i][j] = fac[j][i];
  112.         }
  113.     }
  114.   d = determinant(num, r);
  115.   for (i = 0;i < r; i++)
  116.     {
  117.      for (j = 0;j < r; j++)
  118.        {
  119.         inverse[i][j] = b[i][j] / d;
  120.         }
  121.     }
  122.    printf("\n\n\nThe inverse of matrix is : \n");
  123.  
  124.    for (i = 0;i < r; i++)
  125.     {
  126.      for (j = 0;j < r; j++)
  127.        {
  128.          printf("\t%f", inverse[i][j]);
  129.         }
  130.     printf("\n");
  131.      }
  132. }

$ gcc inverse_matrix.c -o inverse_matrix
$ ./inverse_matrix
 
Enter the order of the Square Matrix : 3
 
Enter the elements of 3X3 Matrix : 3 5 2 1 5  8 3 9 2
 
The inverse of matrix is : 
        0.704545       -0.090909    -0.340909
       -0.250000       -0.000000     0.250000
        0.068180        0.136364    -0.113636

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.