为什么行列式的结果总是为0?

时间:2018-03-17 13:10:14

标签: c++

有没有人知道为什么行列式的结果总是为0?有人可以帮我解决这个问题,或者至少告诉我这个导致行列式价值不会出现的代码有什么问题吗?谢谢,好人:)

#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;
}

0 个答案:

没有答案