15,798,111 members

See more:

Okay I'm trying to perform matrix operations on a few matrices that I read in from a file. To store the dimensions of each matrix I used a 2d array, and I also used a multiD array to specify each element of a particular matrix. The part I'm having a problem with is trying to calculate the determinant of one of the matrices. The file will tell which matrix I must do this for: so something like 8D, where the 8 represents the specific matrix. Once my program reads the D is calls my determinant function. Here is the code I have for this function:

the sum+= part is the part that I think is causes my trouble. I at first had declared determinant[25][25] so instead of determinant[h][k]=

myArray[matrix][i][j] I had the myArray equal to determinant[matrix][h][k]. With this, my program was able to compile, however sometimes my program would crash and when i tried to debug it it told me that there was problems with my determinant array. Which is why I tried to change it. Even though the first way made my program not work sometimes, it still worked other times except the number calculated should have been 220.00 and instead it was 022000. The way the code currently is written doesn't work, for obvious reasons though -- determinant is only a 2D array and yet it takes the spot of a 3D array in the call to the determinant function while performing the sum calculation. I' kinda confused, and don't really know where to go. It might be a quick fix, or I have some big logical mistake, not really sure. ANY help would be greatly appreciated!

C#

void Determinant(float myArray[25][25][25], float otherArray[25][25],int c) { int matrix; matrix=c-1; float sum=0; float determinant[25][25]; if (otherArray[matrix][0]!=otherArray[matrix][1]) { cout<<"The determinant could not be found because the matrix was not a square matrix."<<"\n"<<endl; return; } if(otherArray[matrix][0]==2) { sum=(myArray[matrix][0][0]*myArray[matrix][1][1])-(myArray[matrix][0][1]*myArray[matrix][1][0]); cout<<sum; } for (int p=0; p<otherArray[matrix][0]; p++) { int h=0; int k=0; for (int i=1; i<otherArray[matrix][0]; i++) { for (int j=0; j<otherArray[matrix][0]; j++) { if (j==p) continue; determinant[h][k]=myArray[matrix][i][j]; k++; if (k==(otherArray[matrix][0]-1)) { h++; k=0; } } } sum+=myArray[matrix][0][p]*pow(-1.0,p)*Determinant(determinant,otherArray-1, matrix); } cout<<"The determinant of Matrix "<<c<<" is : "<<sum<<endl; cout<<"\n\n"<<endl; return; }

the sum+= part is the part that I think is causes my trouble. I at first had declared determinant[25][25] so instead of determinant[h][k]=

myArray[matrix][i][j] I had the myArray equal to determinant[matrix][h][k]. With this, my program was able to compile, however sometimes my program would crash and when i tried to debug it it told me that there was problems with my determinant array. Which is why I tried to change it. Even though the first way made my program not work sometimes, it still worked other times except the number calculated should have been 220.00 and instead it was 022000. The way the code currently is written doesn't work, for obvious reasons though -- determinant is only a 2D array and yet it takes the spot of a 3D array in the call to the determinant function while performing the sum calculation. I' kinda confused, and don't really know where to go. It might be a quick fix, or I have some big logical mistake, not really sure. ANY help would be greatly appreciated!

You shouldn't use recursion to compute a determinant... Anyway, I would recommend LAPACK or ATLAS.

Permalink

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900