C++ Program to Check if a Matrix is Invertible

«
»
This is a C++ Program to check if the matrix is invertible or not. It is invertible only if its determinant is non-zero.

Here is source code of the C++ Program to Check if a Matrix is Invertible. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<conio.h>
  2. #include<iostream>
  3. #include<math.h>
  4.  
  5. using namespace std;
  6. double d = 0;
  7. double det(int n, double mat[10][10]);
  8. double det(int n, double mat[10][10])
  9. {
  10.     double submat[10][10];
  11.     if (n == 2)
  12.         return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
  13.     else
  14.     {
  15.         for (int c = 0; c < n; c++)
  16.         {
  17.             int subi = 0; //submatrix's i value
  18.             for (int i = 1; i < n; i++)
  19.             {
  20.                 int subj = 0;
  21.                 for (int j = 0; j < n; j++)
  22.                 {
  23.                     if (j == c)
  24.                         continue;
  25.                     submat[subi][subj] = mat[i][j];
  26.                     subj++;
  27.                 }
  28.                 subi++;
  29.  
  30.             }
  31.             d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
  32.         }
  33.     }
  34.     return d;
  35. }
  36. int main(int argc, char **argv)
  37. {
  38.  
  39.     cout << "Enter the dimension of the matrix:\n";
  40.     int n;
  41.     cin >> n;
  42.     double mat[10][10];
  43.     cout << "Enter the elements of the matrix:\n";
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         for (int j = 0; j < n; j++)
  47.         {
  48.             cin >> mat[j][i];
  49.         }
  50.     }
  51.     if (det(n, mat) != 0)
  52.     {
  53.         cout << "The given matrix is invertible";
  54.     }
  55.     else
  56.     {
  57.         cout << "The given matrix is not invertible";
  58.     }
  59. }

Output:

advertisement
$ g++ InvertibilityOfMatrix.cpp
$ a.out
 
Enter the dimension of the matrix:
3
Enter the elements of the matrix:
1 2 3 
4 5 6
7 8 9
The given matrix is not invertible
 
Enter the dimension of the matrix:
5
Enter the elements of the matrix:
1 2 3 4 5
6 7 8 9 0
0 9 8 7 6
5 4 3 2 1
1 3 5 7 9
The given matrix is invertible

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.