I wrote up a recursive function for computing a determinant. I know I could have done it much more efficiently but that is not the point here. I have a variable called "det1" that holds the final value for the determinant at the end of the recursion. The weird part is when I return this value in the det function, I get complete rubbish. BUT, when I just plain and simply print "det1" out, I get my answer. Any guesses here?
int det1 = 0;
int p = 0;
int det(vector<vector<int> > (&A)){
if (A.size() != A[0].size()){
cout << "Determinant Error: non-square matrix. \n";
return 0;
}
int cF;
vector<vector<int> > temp01;
if (A.size() == 2){
det1 += (A[0][0]*A[1][1]-A[0][1]*A[1][0]);
//cout << "Determinant : " << det1 << "\n";
int output = det1; ///////////////////////////////////////Problem with final return
//cout << "Recursion Count : " << p << "\n";
//return(output); ///////////////////////////////////////
}else{//extract until a 2x2 is reached
for (int i = 0; i < A.size(); i++){
temp01 = extractNext(A,0, i);
//printMatrix(temp01);
cF = pow(-1, (0)+(i))*A[0][i];
//cout << "Cofactor : " << cF << "\n";
for (int j = 0; j< temp01.size(); j++){
temp01[0][j] = cF*temp01[0][j]; //account for cofactor by multiplying it in
}
//printMatrix(temp); cout << "\n";
p++;
det(temp01);
}
}
}