C++ Program to Find Determinant of a Matrix

This C++ program displays the value of the determinant of a particular matrix. A determinant is a value associated with a square matrix. It can be computed from the entries of the matrix by a specific arithmetic expression, while other ways to determine its value exist as well.

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

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

Output:

$ g++ DeterminantOfMatrix.cpp
$ a.out
 
Enter the dimension of the matrix:
3
Enter the elements of the matrix:
3 5 2
8 4 8
2 4 7
The determinant of the given matrix is: -164
 
Enter the dimension of the matrix:
4
Enter the elements of the matrix:
9 5 2 5 
9 5 3 7
6 5 4 8
1 5 3 7
The determinant of the given matrix is: 0

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

advertisement
advertisement

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

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.