Determinant of a Matrix in C++?

C++Server Side ProgrammingProgramming

The determinant of a matrix can be calculated only for a square matrix by multiplying the first row cofactor by the determinant of the corresponding cofactor and adding them with alternate signs to get the final result.

$$A = \begin{bmatrix}a & b & c\\d & e &f \\g & h &i \\ \end{bmatrix} |A| = a(ei-fh)-b(di-gf)+c(dh-eg)$$

First we have the determinantOfMatrix(int mat[N][N], int dimension) function that takes the matrix and the dimension value of the matrix. If the matrix is of only 1 dimension then it returns the [0][0] matrix value. This condition is also used as a base condition since we recursively iterate our matrix with reducing the dimensions on each recursive call.

int determinantOfMatrix(int mat[N][N], int dimension){
int Det = 0;
if (dimension == 1)
return mat[0][0];

We then declare the cofactorMat[N][N] which will be passed to the cofactor(int mat[N][N], int temp[N][N], int p, int q, int n) function till the firstRow is less then the dimension. The determinant of the matrix is stored in the Det variable with signs alternating on each for loop iteration. This det is then returned to the main function where it is printed.

int cofactorMat[N][N];
int sign = 1;
for (int firstRow = 0; firstRow < dimension; firstRow++){
cofactor(mat, cofactorMat, 0, firstRow, dimension);
Det += sign * mat[0][firstRow] * determinantOfMatrix(cofactorMat, dimension - 1);
sign = -sign;
}
return Det;
}

The cofactor(int mat[N][N], int temp[N][N], int p,int q, int n) function takes the matrix, the cofactor matrix, 0, firstRow value and the dimension of the matrix as parameter values. The nested for loop then helps us iterate over the matrix and where the p & q values are not equal to row and column values respectively, those values are stored in the temp matrix.

void cofactor(int mat[N][N], int temp[N][N], int p,int q, int n){
int i = 0, j = 0;
for (int row = 0; row < n; row++){
for (int column = 0; column < n; column++){
if (row != p && column != q){
temp[i][j++] = mat[row][column];

Once the row gets filled we increase the row index and reset the col index.

if (j == n - 1){
j = 0;
i++;
}

Finally we have our display(int mat[N][N], int row, int col) that takes the matrix and no of rows and columns and iterate over the matrix as 2d array and print those values on each row and column.

void display(int mat[N][N], int row, int col){
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++)
cout<<mat[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}

Example

Let us look at the following implementation to find the determinant of the matrix.

Live Demo

#include <iostream>
using namespace std;
const int N = 3;
void cofactor(int mat[N][N], int temp[N][N], int p,int q, int n){
int i = 0, j = 0;
for (int row = 0; row < n; row++){
for (int column = 0; column < n; column++){
if (row != p && column != q){
temp[i][j++] = mat[row][column];
if (j == n - 1){
j = 0;
i++;
}
}
}
}
}
int determinantOfMatrix(int mat[N][N], int dimension){
int Det = 0;
if (dimension == 1)
return mat[0][0];
int cofactorMat[N][N];
int sign = 1;
for (int firstRow = 0; firstRow < dimension; firstRow++){
cofactor(mat, cofactorMat, 0, firstRow, dimension);
Det += sign * mat[0][firstRow] * determinantOfMatrix(cofactorMat, dimension - 1);
sign = -sign;
}
return Det;
}
void display(int mat[N][N], int row, int col){
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++)
cout<<mat[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
int main(){
int mat[3][3] = {
{ 1, 0, 2},
{ 3, 0, 0},
{ 2, 1, 4}};
cout<<"The matrix is "<<endl;
display(mat,3,3);
cout<<"Determinant of the matrix is "<<determinantOfMatrix(mat, N);
return 0;
}

Output

The above code will produce the following output −

The matrix is
1 0 2
3 0 0
2 1 4

Determinant of the matrix is 6
Published on 16-Jan-2021 08:56:12