Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
//Matrix Determinant and Inverse program
cout << "Matrix Determinant and Inverse program.\n\n";
cout << "Input values for the 2x2 matrix in the following order:\n";
cout << "[d11 d12]\n[d21 d22]\n\n";
cout << "Inputting values of the matrix...\n\n";
//the following doubles are vars for the outputted equation above
double d11;
cout << "d11=";
cin >> d11;
cout << "\n";
double d12;
cout << "d12=";
cin>> d12;
cout << "\n";
double d21;
cout << "d21=";
cin>> d21;
cout << "\n";
double d22;
cout << "d22=";
cin>> d22;
cout << "\n";
//determinant var
double dDet;
dDet=(d11*d22)-(d12*d21);
//in the next set of variables, the original values of the matrix will be
//made into a transverse or whatever the hell it's called.
double dInv11;
dInv11=d22;
double dInv12;
dInv12=d11*-1;
double dInv21;
dInv21=d21*-1;
double dInv22;
dInv22=d11;
// the next set converts the transverse values into the inverse by multiplying
// them by the determinant.
double dDetInv11;
dDetInv11=(1/dDet)*dInv11;
double dDetInv12;
dDetInv12=(1/dDet)*dInv12;
double dDetInv21;
dDetInv21=(1/dDet)*dInv21;
double dDetInv22;
dDetInv22=(1/dDet)*dInv22;
//outputs the answers
cout << "Calculating the determinant...\n";
cout << "Determinant=";
cout << dDet;
cout << "\n\n";
cout << "Calculating the inverse of the matrix...\n";
cout << "Answer=\n[";
cout << dDetInv11;
cout << " ";
cout << dDetInv12;
cout << "]\n[";
cout << dDetInv21;
cout << " ";
cout << dDetInv22;
cout << "]\n\n\n";
system("PAUSE");
return 0;
}
It's running pretty smoothly now. Here's a link if anyone wants to see what it does/looks like:
(it's an .exe, so it probably won't work on a mac/linux unless you have something that allows you to use .exe's or you will convert it. I am on my xp partition right now... and i don't have a cpp compiler on my apple work computers. sorry!)
[URL="www.angelfire.com/planet/lilwing89/matrixsolver101.zip"]matrixsolver101.zip[/URL]
i'm debugging right now and thinking of a way to destroy the extra vars. right now if i input 1's for all the vars, i get a #inf and i'm not exactly sure what that means yet... it probably means that the determinant is 0 and there is no inverse. i'm too lazy to figure it out by hand
plus to fix that i'll need an else case. not something i'm going to do tonight
the other problem, is sometimes the inverse contains decimals. just out of curiousity, is there a way to convert them into fractions with c++?