
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Compute Determinant of a Matrix
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
#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 )); }
- Related Articles
- Determinant of a Matrix in C++ Program
- PyTorch – How to compute the determinant of a square matrix?
- Determinant of a Matrix in C++?
- C++ Program to Compute the Sum of Diagonals of a Matrix
- Python Program to find out the determinant of a given special matrix
- Java Program to Compute the Sum of Diagonals of a Matrix
- JavaScript Program to Efficiently compute sums of diagonals of a matrix
- Golang Program to Compute the Sum of Diagonals of a Matrix
- Swift Program to Compute the Sum of Diagonals of a Matrix
- Finding determinant of a square matrix using SciPy library
- Maximum determinant of a matrix with every values either 0 or n in C++
- Compute the determinant of a Two-Dimensional array in linear algebra in Python
- Compute the determinant for a stack of matrices in linear algebra in Python
- Compute the determinant of an array in linear algebra in Python
- PyTorch – How to compute QR decomposition of a matrix?
