Compute Determinant of a Matrix in C++



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.

Steps to Compute Determinant of a Matrix

We find the determinant using a method called recursive Laplace expansion. For each number in the first row, we remove its row and column to make a smaller matrix and find that smaller matrix's determinant. Then, we multiply this determinant by that number and add or subtract it depending on its position. It repeats until we reach 2X2 matrix.

Here's how we did it:

  • First, we defined a determinant function that takes a square matrix and its size.
  • Second, if the size is 1X1, we return the single element and if it's 2X2, we return (a*d - b*c).
  • Next, for larger matrices, we create submatrices by removing the row and column of each element in the first row, and call the function recursively on those.
  • Then, we apply the Laplace expansion by summing up each element in the first row multiplied by its smaller matrix's determinant, with plus or minus signs based on their positions.

C++ Program to Compute Determinant of a Matrix

Here's a complete C++ program that uses recursion to compute the determinant of a matrix.

#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;
}

When we run the above program, it asks us to enter the size of the matrix and its elements, then calculates the determinant of the matrix based on the input. Below is a sample output.

Enter the size of the matrix:
2
Enter the elements of the matrix:
5
6
2
9
The entered matrix is:
5 6 
2 9 
Determinant of the matrix is 33

Time Complexity: O(n!) because the function recursively computes determinants of n smaller (n-1)x(n-1) submatrices.

Space Complexity: O(n^3) because of storing submatrices at each recursion level.

Updated on: 2025-05-20T20:11:00+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements