my professor wants me to create a program where it calculates the inverse of 2 matrices. he already made a program to show us what it looks like but i have to make the source file.
here is what he wants it to look like:
The inverse of:
(-2 -5)
(1 3)
is:
(-3 -5)
(1 2)
The inverse of:
(1 2)
(3 4)
is:
(-4 2)
(3 -1)
Press any key to continue . . .
and here is what i get when i run my program using bloodshell devc++:
The inverse of:
(-2 -5)
(1 3)
is:
(-3 -5)
(1 2)
The inverse of:
(1 2)
(3 4)
is:
(-2 1)
(1 0)
Press any key to continue . . .
i get a different inverse for the second matrix
does anyone know how to fix it and explain it, here is my source file:
#include<iostream>
#include<cstdlib>
using namespace std;
All your inputs are declared as integers. That's OK, more or less, if you add, multiply and subtract. But the moment you do a division then you will get truncation errors which means that decimals are cut off. Try declaring your variables as floats instead of ints.
I don't know what the 0.5 is, but the problem with the calculation is that you're using int.
Integer division results in an integer and discards any rest. 3/2 equals 1 rest 1, but the rest is discarded.
For the second matrix, the determinant is -2 d/f == -4/2 == -2 rest 0
-b/f == 2/2 == 1 rest 0
-c/f == 3/2 == 1 rest 1
a/f == -1/2 == 0 rest 1
The first matrix turns out to be correct only because the determinant is 1.