find the determinant of a matrix

I use Laplas to find the determinant, but the result is always 0.
my code:
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#define N 100
int P[N][N];
using namespace std;


void determinant(int M[][N])
{ int n;
int det;
int A[N];
int B[N];
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));

static int t=0;
static int f=0;
static int LHS=0;
static int RHS=0;
cout<<"please input the rows of the elements"<<endl;
cin>>n;
for(int a=0;a<n;a++)
 for(int b=0;b<n;b++)
   P[a][b]=(a+b)%3;
   


cout<<"please input the elements"<<endl;
for(int i=0;i<n;i++)
 for(int j=0;j<n;j++)
      cin>>M[i][j];

for(int i=0;i<n;i++)
{
do{ 

int y=P[i][t++];
A[i]*=M[t++][y];

}
while(t<n);
}    


for(int i=n-1;i>=0;i--)
{
do{ 

int y=P[i][f++];
B[i]*=M[f++][y];

}
while(f<n);
}   
for(int i=0;i<n;i++)
{LHS+=A[i];
}
for(int j=0;j<n;j++)
{RHS+=B[j];
}
det=LHS-RHS;

cout<<"The determinant is"<<det;

 cout<<endl;
}

int main()
{    int M[N][N];
    determinant(M);
    
    system("pause");
    return 0;
}



_ Indent your code
_ Modularize
_ Duck debug (especially lines 12,13,37,49,55,58)
_ Don't "optimize"
Can someone give some correction?
For example instead of

int A[N];
int B[N];
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));

you could write

int A[N] = {};
int B[N] = {};

:)
I corrected so, but it still have bug
Sorry but i do not understand what your function does and have no any desire to investigate such a bad code.

Good luck!
Last edited on
Topic archived. No new replies allowed.