programming-examples/c++/Numerical_Problems/C++ Program to Check if a Matrix is Invertible.cpp
2019-11-18 14:44:36 +01:00

76 lines
1.9 KiB
C++

#include<conio.h>
#include<iostream>
#include<math.h>
using namespace std;
double d = 0;
double det(int n, double mat[10][10]);
double det(int n, double mat[10][10])
{
double submat[10][10];
if (n == 2)
return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
else
{
for (int c = 0; c < n; c++)
{
int subi = 0; //submatrix's i value
for (int i = 1; i < n; i++)
{
int subj = 0;
for (int j = 0; j < n; j++)
{
if (j == c)
continue;
submat[subi][subj] = mat[i][j];
subj++;
}
subi++;
}
d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
}
}
return d;
}
int main(int argc, char **argv)
{
cout << "Enter the dimension of the matrix:\n";
int n;
cin >> n;
double mat[10][10];
cout << "Enter the elements of the matrix:\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> mat[j][i];
}
}
if (det(n, mat) != 0)
{
cout << "The given matrix is invertible";
}
else
{
cout << "The given matrix is not invertible";
}
}
/*
Enter the dimension of the matrix:
3
Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
The given matrix is not invertible
Enter the dimension of the matrix:
5
Enter the elements of the matrix:
1 2 3 4 5
6 7 8 9 0
0 9 8 7 6
5 4 3 2 1
1 3 5 7 9
The given matrix is invertible