C++ Online Compiler
Example: Determinant of NxN Matrix in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Determinant of NxN Matrix #include <iostream> #include <vector> #include <iomanip> // For std::setw // Function to get a sub-matrix (minor matrix) by removing a specified row and column std::vector<std::vector<int>> getSubMatrix(const std::vector<std::vector<int>>& matrix, int row, int col, int N) { std::vector<std::vector<int>> subMatrix(N - 1, std::vector<int>(N - 1)); int sub_i = 0; // Row index for subMatrix for (int i = 0; i < N; ++i) { if (i == row) continue; int sub_j = 0; // Column index for subMatrix for (int j = 0; j < N; ++j) { if (j == col) continue; subMatrix[sub_i][sub_j] = matrix[i][j]; sub_j++; } sub_i++; } return subMatrix; } // Recursive function to calculate the determinant int calculateDeterminant(const std::vector<std::vector<int>>& matrix, int N) { // Base case 1: For a 1x1 matrix, the determinant is the single element if (N == 1) { return matrix[0][0]; } // Base case 2: For a 2x2 matrix, use the formula (ad - bc) if (N == 2) { return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]); } int det = 0; int sign = 1; // Used for alternating signs (+ - + - ...) // Iterate through the first row to apply cofactor expansion for (int j = 0; j < N; ++j) { // Get the sub-matrix by excluding the first row and current column 'j' std::vector<std::vector<int>> subMatrix = getSubMatrix(matrix, 0, j, N); // Recursively calculate the determinant of the sub-matrix // Add or subtract (based on 'sign') the product of the element and its sub-matrix's determinant det += sign * matrix[0][j] * calculateDeterminant(subMatrix, N - 1); // Toggle the sign for the next element sign = -sign; } return det; } // Function to print a matrix (for debugging/display) void printMatrix(const std::vector<std::vector<int>>& matrix, int N) { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { std::cout << std::setw(4) << matrix[i][j] << " "; } std::cout << std::endl; } } int main() { // Step 1: Define the matrix size and elements. // Example 1: 3x3 matrix std::vector<std::vector<int>> matrix3x3 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int N3 = 3; std::cout << "Matrix 1 (" << N3 << "x" << N3 << "):" << std::endl; printMatrix(matrix3x3, N3); std::cout << "Determinant: " << calculateDeterminant(matrix3x3, N3) << std::endl << std::endl; // Example 2: 4x4 matrix std::vector<std::vector<int>> matrix4x4 = { {1, 0, 2, -1}, {3, 0, 0, 5}, {2, 1, 4, -3}, {1, 0, 5, 0} }; int N4 = 4; std::cout << "Matrix 2 (" << N4 << "x" << N4 << "):" << std::endl; printMatrix(matrix4x4, N4); std::cout << "Determinant: " << calculateDeterminant(matrix4x4, N4) << std::endl << std::endl; // Example 3: 2x2 matrix std::vector<std::vector<int>> matrix2x2 = { {4, 2}, {-1, 3} }; int N2 = 2; std::cout << "Matrix 3 (" << N2 << "x" << N2 << "):" << std::endl; printMatrix(matrix2x2, N2); std::cout << "Determinant: " << calculateDeterminant(matrix2x2, N2) << std::endl << std::endl; // Example 4: 1x1 matrix std::vector<std::vector<int>> matrix1x1 = { {7} }; int N1 = 1; std::cout << "Matrix 4 (" << N1 << "x" << N1 << "):" << std::endl; printMatrix(matrix1x1, N1); std::cout << "Determinant: " << calculateDeterminant(matrix1x1, N1) << std::endl << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS