0

I have managed to do the determinant and have a person enter their own matrix and that works fine. I am trying to use the inverse by making a second matrix "matb" and use it as the inverse as seen below. mat[3][3] was my declared matrix and the numbers are selected by the user, im using :

#include<math.h>

at the top but i cant see why my code won't let me use the values from "mat" to set matb :

int matb[3][3]= { {(mat[1][1] * mat[2][2]) - (mat[1][2] * mat[2][1]) , (mat[1][2] * mat[2][0]) - (mat[1][0] * mat[2][2]) ,(mat[1][0] * mat[2][1]) - (mat[1][1] * mat[2][0]) } , { (mat[0][2] * mat[2][1]) - (mat[0][1] * mat[2][2]) ,(mat[0][0] * mat[2][2]) - (mat[0][2] * mat[2][0]) ,(mat[0][1] * mat[2][0]) - (mat[0][0] * mat[2][1]) } , { (mat[0][1] * mat[1][2]) - (mat[0][2] * mat[1][1]), (mat[0][2] * mat[1][0]) - (mat[0][0] * mat[1][2]),(mat[0][0] * mat[1][1] )- (mat[0][1] * mat[1][0]) } }; 

It is a long bit of code but mathematically it is correct so im assuming it is something in the code that i have done wrong.

I don't get any compiling error, when i print it to screen, it comes up as a string of letters and numbers, and when used in further calculations the further calculations are very wrong indeed.

This isnt a duplicate as i am not trying to print the inverse, i just printed it to check the code is correct, and it isnt.

int matb[3][3]= { { mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1], mat[1][2] * mat[2][0] - mat[1][0] * mat[2][2], mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0] }, { mat[0][2] * mat[2][1] - mat[0][1] * mat[2][2], mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0], mat[0][1] * mat[2][0] - mat[0][0] * mat[2][1] }, { mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1], mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2], mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0] } };

            cout << matb[x][y] << endl;         //somethings wrong, not doing it properly


            for (x = 0; x < 3; x++)
                for (y = 0; y < 3; y++)
            {
                matbt[y][x] = matb[x][y]; // wrong from matb
            }

        inverse = determinant*matbt[3][3];

        cout << inverse << endl;

That is my code, the rest is the standard return(0) _getch() ect

BiGubbs
  • 15
  • 4
  • Are you getting some sort of compiler error? – NathanOliver May 18 '17 at 19:13
  • No there is no compiler error, it gives me a string of letters and numbers when i cout< – BiGubbs May 18 '17 at 19:15
  • 1
    You can't just use `cout< – NathanOliver May 18 '17 at 19:16
  • i used cout< – BiGubbs May 18 '17 at 19:41
  • Please post a [mcve] so we know exactly what is going on. Include the data to use. The expected output and the output you are getting. – NathanOliver May 18 '17 at 19:42
  • This code should help you out: http://ideone.com/9kCZeT – Christopher Oicles May 18 '17 at 22:44
  • When you have not working cofe: then put up an [mcve]. As of now, we can't help. – GhostCat May 19 '17 at 03:22
  • I worked out my problem , i was not using the loop correctly to output the matrix, and it was only using the first value that was output in future calculations. To help anyone else that has this problem this was the working output: for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { matbt[y][x] = matb[x][y]; } } double inverse[3][3]; for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { inverse[3][3] = (1 / determinant)*matbt[x][y]; cout << inverse << "\t"; fileout << inverse << "\t"; } cout << endl; } – BiGubbs May 20 '17 at 13:40
  • Thank you for all of your help. – BiGubbs May 20 '17 at 13:56

0 Answers0