C++ Program to Check if a Matrix is Invertible

This is a C++ Program to Find if a Matrix is Invertible or Not.

Problem Description

The program takes a matrix and checks if it is invertible. An invertible matrix is a square matrix whose determinant exists.

Problem Solution

1. The program takes a square matrix.
2. Using a function, determinant of the matrix is calculated.
3. If determinant is equal to 0, it is not an invertible matrix.
4. Else it is an invertible matrix.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if a Matrix is Invertible or Not. The program output is shown below.

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

1. The user is asked to enter the order of the matrix, i.e the size. It is stored in the variable ‘n’.
2. The elements of the matrix are asked to enter and stored in ‘mat’.
3. Using a function ‘det’, the determinant of the matrix is calculated.
4. In the main function, the value of determinant is returned. If it is equal to 0, the matrix is not invertible.
5. Else the entered matrix is invertible. The matrix is printed.
6. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter order of the matrix : 2
Enter the elements of the matrix : 2 -3 -1 2
The given matrix is invertible.
 2 -1
 -3 2
 
Case 2 :
Enter order 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.
 1 4 7
 2 5 8
 3 6 9
 
Case 3 :
Enter order of the matrix : 3
Enter the elements of the matrix : 1 0 0 0 1 0 0 0 1
The given matrix is invertible.
 1 0 0
 0 1 0
 0 0 1

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

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.