I have been looking for code on how to create a matrix determinant calculator, and I found code from ( Matrix determinant algorithm C++ ).
I adopted the code to try it, but i realized that i did not know how to pass in a multidimensional array into the function, without defining its dimensions first (i got errors).
So, can you please show me how to pass a multidimensional array into a function without defining its dimensions first (i.e how int a[MAX][MAX]
is an argument, what is 'MAX').
I hope my question is clear, thank you for your help.
This is the (edited) code: -sample input would be a square matrix and its size.
int determinant(int oMat[][], int n){
int sMat[n][n]; //Temporary matrix
int det = 0; //Initializing 'det'.
if(n==1){
det = oMat[0][0]; //Calculating.
return det;
}else if(n==2){
det = oMat[0][0]*oMat[1][1] - oMat[1][0]*oMat[0][1]; //Formula for 2x2.
return det;
}else{
for(int p=0; p<n; p++){ //Selecting 'oMat' row one.
int k=0; //'sMat' rows.
int m=0; //'sMat' columns.
//Creating the temporary matrix 'sMat'.
for(int i=1; i<n; i++){ //for 'oMat' rows.
for(int j=0; j<n; j++){
if(j==p){
continue;
}
sMat[k][m] = oMat[i][j]; m++;
if(m==n-1){
k++; //Go to the next row.
m = 0; //Start at column one (index 0).
}
}
}
det = det + oMat[0][p] * pow(-1,p) * determinant(sMat, n-1);
}
return det;
}
}