From 4cfbd5367a7c23495bac1cce4e7518a31ce602bd Mon Sep 17 00:00:00 2001 From: Arnav2708 <95859782+Arnav2708@users.noreply.github.com> Date: Fri, 7 Oct 2022 17:49:08 +0530 Subject: [PATCH] Create Determinant_Solver.cpp This code can solve any 3x3 determinant. --- Determinant_Solver.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Determinant_Solver.cpp diff --git a/Determinant_Solver.cpp b/Determinant_Solver.cpp new file mode 100644 index 0000000..d3db252 --- /dev/null +++ b/Determinant_Solver.cpp @@ -0,0 +1,44 @@ +#include +#include +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:"<