C++ Program to Find Inverse of a Graph Matrix

«
»
This C++ program displays the Gauss Jordan method of computing inverse of a matrix. Gauss Jordan method proceeds with row by row reduction of matrix to unit matrix column-wise.

Here is the source code of the C++ program to display the augmented matrix along with the inverse of the matrix taken as input. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Find Inverse of a Graph Matrix
  3.  */
  4. #include<iostream>
  5. #include<conio.h>
  6. #include<stdio.h>
  7. using namespace std;
  8. int main()
  9. {
  10.     int i, j, k, n;
  11.     float a[10][10] = {0},d;
  12.     cout<<"Enter the order of matrix ";
  13.     cin>>n;
  14.     cout<<"Enter the elements\n";
  15.     for (i = 1; i <= n; i++)
  16.     {
  17.         for (j = 1; j <= n; j++)
  18.         {
  19.             cin>>a[i][j];
  20.         }
  21.     }    
  22.     for (i = 1; i <= n; i++)
  23.     {
  24.         for (j = 1; j <= 2 * n; j++)
  25.         {
  26.             if (j == (i + n))
  27.             {
  28.                 a[i][j] = 1;
  29.             }
  30.         }
  31.     }
  32.     for (i = n; i > 1; i--)
  33.     {
  34.         if (a[i-1][1] < a[i][1])
  35.         {
  36.             for(j = 1; j <= n * 2; j++)
  37.             {
  38.                 d = a[i][j];
  39.                 a[i][j] = a[i-1][j];
  40.                 a[i-1][j] = d;
  41.             }
  42.         }
  43.     }
  44.     cout<<"Augmented Matrix: "<<endl;
  45.     for (i = 1; i <= n; i++)
  46.     {
  47.         for (j = 1; j <= n * 2; j++)
  48.         {
  49.             cout<<a[i][j]<<"    ";
  50.         }
  51.         cout<<endl;
  52.     }
  53.     for (i = 1; i <= n; i++)
  54.     {
  55.         for (j = 1; j <= n * 2; j++)
  56.         {
  57.             if (j != i)
  58.             {
  59.                 d = a[j][i] / a[i][i];
  60.                 for (k = 1; k <= n * 2; k++)
  61.                 {
  62.                     a[j][k] = a[j][k] - (a[i][k] * d);
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     for (i = 1; i <= n; i++)
  68.     {
  69.         d=a[i][i];
  70.         for (j = 1; j <= n * 2; j++)
  71.         {
  72.             a[i][j] = a[i][j] / d;
  73.         }
  74.     }
  75.     cout<<"Inverse Matrix "<<endl;
  76.     for (i = 1; i <= n; i++)
  77.     {
  78.         for (j = n + 1; j <= n * 2; j++)
  79.         {
  80.             cout<<a[i][j]<<"    ";
  81.         }
  82.         cout<<endl;
  83.     }
  84.     getch();
  85. }

advertisement
Output
 
Enter the order of matrix 3
Enter the elements
4
2
7
5
1
8
9
4
7
Augmented Matrix:
9    4    7    0    0    1
4    2    7    1    0    0
5    1    8    0    1    0
Inverse Matrix
-0.490196    0.27451    0.176471
0.72549    -0.686274    0.0588236
0.215686    0.0392157    -0.117647

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

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.