C++ Program to Compute Determinant of a Matrix

C++ProgrammingServer Side Programming

The determinant of a square matrix can be computed using its element values. The determinant of a matrix A can be denoted as det(A) and it can be called the scaling factor of the linear transformation described by the matrix in geometry.

An example of the determinant of a matrix is as follows.

The matrix is:
3 1
2 7
The determinant of the above matrix = 7*3 - 2*1
= 21 - 2
= 19
So, the determinant is 19.

The program to compute the determinant of a matrix is as follows.

Example

 Live Demo

#include<iostream>
#include<math.h>
using namespace std;
int determinant( int matrix[10][10], int n) {
   int det = 0;
   int submatrix[10][10];
   if (n == 2)
   return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
   else {
      for (int x = 0; x < n; x++) {
         int subi = 0;
         for (int i = 1; i < n; i++) {
            int subj = 0;
            for (int j = 0; j < n; j++) {
               if (j == x)
               continue;
               submatrix[subi][subj] = matrix[i][j];
               subj++;
            }
            subi++;
         }
         det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ));
      }
   }
   return det;
}
int main() {
   int n, i, j;
   int matrix[10][10];
   cout << "Enter the size of the matrix:\n";
   cin >> n;
   cout << "Enter the elements of the matrix:\n";
   for (i = 0; i < n; i++)
   for (j = 0; j < n; j++)
   cin >> matrix[i][j];
   cout<<"The entered matrix is:"<<endl;
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++)
      cout << matrix[i][j] <<" ";
      cout<<endl;
   }
   cout<<"Determinant of the matrix is "<< determinant(matrix, n);
   return 0;
}

Output

Enter the size of the matrix: 3
Enter the elements of the matrix:
7 1 3
2 4 1
1 5 1
The entered matrix is:
7 1 3
2 4 1
1 5 1
Determinant of the matrix is 10

In the above program, the size and elements of the matrix are provided in the main() function. Then the function determinant() is called. It returns the determinant of the matrix which is displayed. This is demonstrated with the following code snippet.

cout << "Enter the size of the matrix:\n";
cin >> n;
cout <<"Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < n; i++) {
   for (j = 0; j < n; j++)
   cout << matrix[i][j] <<" ";
   cout<<endl;
}
cout<<"Determinant of the matrix is "<< determinant(matrix, n);

In the function determinant(), if the size of the matrix is 2, then the determinant is directly calculated and the value is returned. This is shown as follows.

if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));

If the size of the matrix is not 2, then the determinant is calculated recursively. There are 3 nested for loops used with the loop variables x, i and j. These loops are used to calculate the determinant and the function determinant() is called recursively to calculate the inner determinant and then multiply it with the outer value. This is demonstrated by the following code snippet.

for (int x = 0; x < n; x++) {
   int subi = 0;
   for (int i = 1; i < n; i++) {
      int subj = 0;
      for (int j = 0; j < n; j++) {
         if (j == x)
         continue;
         submatrix[subi][subj] = matrix[i][j];
         subj++;
      }
      subi++;
   }
   det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ));
}
raja
Published on 05-Oct-2018 11:29:56
Advertisements