Write a C++ Program to find the determinant of a 2 * 2 Matrix with an example. The math formula to calculate Matrix determinant of 2*2 and 3*3
#include<iostream>
using namespace std;
int main()
{
int rows, columns, determinant, determMatrix[2][2];
cout << "\nPlease Enter the 2 * 2 Matrix Items\n";
for(rows = 0; rows < 2; rows++)
{
for(columns = 0; columns < 2; columns++)
{
cin >> determMatrix[rows][columns];
}
}
determinant = ((determMatrix[0][0] * determMatrix[1][1]) -
(determMatrix[0][1] * determMatrix[1][0]));
cout << "\nThe Determinant of 2 * 2 Matrix = " << determinant;
return 0;
}
C++ Program to find the determinant of a 3 * 3 Matrix
#include<iostream>
using namespace std;
int main()
{
int x, y, z, rows, columns, determinant, dMatrix[3][3];
cout << "\nPlease Enter the 3 * 3 Matrix Items\n";
for(rows = 0; rows < 3; rows++)
{
for(columns = 0; columns < 3; columns++)
{
cin >> dMatrix[rows][columns];
}
}
x = ((dMatrix[1][1] * dMatrix[2][2]) - (dMatrix[2][1] * dMatrix[1][2]));
y = ((dMatrix[1][0] * dMatrix[2][2]) - (dMatrix[2][0] * dMatrix[1][2]));
z = ((dMatrix[1][0] * dMatrix[2][1]) - (dMatrix[2][0] * dMatrix[1][1]));
determinant = ((dMatrix[0][0] * x) - (dMatrix[0][1] * y) + (dMatrix[0][2] * z));
cout << "\nThe Determinant of 3 * 3 Matrix = " << determinant;
return 0;
}