Hello,

I've found this forum quite helpful as a newbie. I'm hoping someone can help me with this:

/*

This program finds the inverse of a 3x3 matrix.

*/

#include <iostream>
using namespace std;

int main() {

double a,b,c,d,e,f,g,h,i,j,k;
double c_1,c_2,c_3,c_4,c_5,c_6,c_7,c_8,c_9;

cout << "Enter the first row of the matrix (separate with spaces): ";
cin >> a >> b >> c;

cout << "Enter the second row of the matrix (separate with spaces): ";
cin >> d >> e >> f;

cout << "Enter the final row of the matrix (separate with spaces): ";
cin >> g >> h >> i;

j = a * (e*i-h*f) - b * (d*i-g*f) + c * (d*h-e*g);

if(j=0){
cout << "This is a singular matrix. There is no inverse." << "\n";
}


else{
k = 1/j;

c_1 = e*i-h*f; c_2 = -( d*i-g*f); c_3 = d*h-g*e;
c_4 = -( b*i-h*c ); c_5 = a*1-g*c; c_6 = -( a*h-b*g );
c_7 = b*f-e*c; c_8 = -( a*f-d*c ); c_9 = a*e - b*d;

cout << "[" << a << "\t" << b << "\t" << c << "]" << "^-1" << "\t";
cout << "\t" << "[" << c_1 << "\t" << c_4 << "\t" << c_7 << "]" << "\n";

cout << "|" << d << "\t" << e << "\t" << f << "|" << " = " << "\t";
cout << k << "\t" << "|" << c_2 << "\t" << c_5 << "\t" << c_8 << "|" << "\n";

cout << "[" << g << "\t" << h << "\t" << i << "]" << "\t";
cout << "\t" << "[" << c_3 << "\t" << c_6 << "\t" << c_9 << "]" << "\n";
}


return 0;

}


It's quite long!

The best way of understanding it is to run it and input the identity matrix.
The inverse matrix is almost correct, the problem is that I keep getting "inf" as 1/determinant when it isn't always the case.

The error lies with my "for" statement (since inputting a 3x3 matrix with only one value as 1 doesn't give the "singular matrix" output). I can't see why it's wrong though.

Any help would be great!