[Why does the result for determinant is always 0?]
Mar 17, 2018 at 12:58pm UTC
Is there anyone know why is the result of determinant is always 0? Thank you, Good people :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include<iostream>
using namespace std;
int cofactor(int data[3][3],int x,int y)
{
float cofactor_v;
cofactor_v = data[(x + 1) % 3][(y + 1) % 3]
* data[(x + 2) % 3][(y + 2) % 3]
- data[(x + 1) % 3][(y + 2) % 3]
* data[(x + 2) % 3][(y + 1) % 3];
return cofactor_v;
}
int main(){
int mat[3][3], i, j;
float determinant = 0, cofactor_v;
cout<<"Enter elements of matrix row wise:\n" ;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin>>mat[i][j];
printf("\nGiven matrix is:" );
for (i = 0; i < 3; i++){
cout<<"\n" ;
for (j = 0; j < 3; j++)
{
cout<<mat[i][j];
}
}
for (i = 0; i < 1; i++){
cout<<"\n" ;
for (j = 0; j < 1; j++)
{
cout<<"\Nilai kofaktor matriks adalah=" <<cofactor(mat,0,0)<<cofactor(mat,1,1)<<cofactor(mat,0,2);
}
}
//finding determinant
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == 0)
determinant += mat[0][j] * cofactor_v;
}
}
cout<<"Determinan matriks adalah=" <<determinant;
}
Mar 17, 2018 at 1:13pm UTC
Try
1 2 3 4 5 6
//finding determinant
for (j = 0; j < 3; j++)
{
determinant += mat[0][j] * cofactor(mat,0,j);
}
cout<<"Determinan matriks adalah=" <<determinant;
If you are expanding using the first row then you probably want to change the middle cofactor that you are printing out on line 37 as well (and put some spaces in your output).
Last edited on Mar 17, 2018 at 4:40pm UTC
Mar 17, 2018 at 1:17pm UTC
Last edited on Mar 17, 2018 at 1:17pm UTC
Topic archived. No new replies allowed.