I have this following program which calculates the determinant of an NxN matrix, the program itself works, but when I try to calculate the 2nd order matrix determinant at the end of recursion, accessing the 2d 2x2 array elements gives me wrong values.
I am using recursion to be able to calculate the determinant of higher-order matrices, findDet() is the recursive function, that uses a loop to call itself for each element of the first row in a given matrix and sub-matrices if their order is greater than two.
/******************************************************************************
Program to find the determinant a matrix
*******************************************************************************/
#include <stdio.h>
#define N 3
long int findDet(int arr[][N], size_t order); // To find the determinant
void initNewArr(int newArr[][N-1], int prevArr[][N], size_t order, int exCol); // To create sub matrices
int main()
{
int order = N;
printf("This program is to find determinant of a given matrix.\n\
\rNumber of row and column is going to be equal.\n");
// printf("Enter the order of the matrix: ");
// scanf("%d", &order);
// Read the input matrix from the user
int matrix[order][order];
printf("Enter matrix elements...\n");
for(int i = 0; i < order; i++) {
for(int j = 0; j < order; j++) {
printf("Enter matrix[%d][%d]: ", i+1, j+1);
scanf("%d", &matrix[i][j]);
}
}
// Print the matrix
printf("New Matrix itself...\n");
for(int i = 0; i < order; i++) {
for(int j = 0; j < order; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Calling findDet() to calculate determinant and store it in "det"
long int det = findDet(matrix, order);
// Print the determinant
printf("\nDeterminant of the matrix = %li", det);
return 0;
}
long int findDet(int arr[][N], size_t order) {
// if order is 2 calculate the determinant of 2nd order matrix
if(order == 2) {
int detS = ((arr[0][0]) * (arr[1][1])) - ((arr[0][1]) * (arr[1][0]));
/*
This following expression shows that accessed values are not correct
for this reason, the calculated determinant of 2nd order matrix is
not correct.
The whole problem is in this block of code
*/
printf("=== arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0] = %d * %d - %d * %d\n", arr[0][0], arr[1][1], arr[0][1], arr[1][0]);
printf("Result of above expression === %d\n", detS);
return detS;
} // if order is higher then continue to break down matrix to 2nd order step by step
long int det = 0;
/*
The following for loop is for multiplying each element of
first row by determinant of sub-matrix,
which in case of below 3x3 matrix:
2 -3 1
2 0 -1 its determinant = 2 * (det of sub-matrix1) - (-3) * (det of sub-matrix2) + 1 * (det of sub-matrix3)
1 4 5
sub-matrices are:
>>> sub-matrix1
0 -1
4 5 --> in the first iteration of the array inside findDet()
while accessing the
arr[0][0] gives value 0
arr[0][1] gives value -1
arr[1][0] gives value 5
arr[1][1] gives value 1
>>> sub-matrix2
2 -1
1 5 --> in the second iteration of the array inside findDet()
while accessing the
arr[0][0] gives value 2
arr[0][1] gives value -1
arr[1][0] gives value 5
arr[1][1] gives value 1
>>> sub-matrix3
2 0
1 4 --> in the third iteration of the array inside findDet()
while accessing the
arr[0][0] gives value 2
arr[0][1] gives value 0
arr[1][0] gives value 4
arr[1][1] gives value 1
But since we get wrong values for sub-matrices the final determinant is not correct
*/
for(int i = 0; i < order; i++) {
// New sub matrix in each iteration
int newArr[order-1][order-1];
initNewArr(newArr, arr, order, i);
// Print the sub matrix
printf("\nNewly Created Sub Matrix itself...\n");
for(int i = 0; i < order-1; i++) {
for(int j = 0; j < order-1; j++) {
printf("%d\t", newArr[i][j]);
}
printf("\n");
}
// Calculate Determinant
if(i % 2 == 0) { // if i is 0 or even which then becomes odd-th element of matrix add result to det
det += (arr[0][i] * findDet(newArr, order-1));
} else { // otherwise subtract the result from the det
det -= (arr[0][i] * findDet(newArr, order-1));
}
}
return det;
} // ================== findDet()
void initNewArr(int newArr[][N-1], int prevArr[][N], size_t order, int exCol) {
for(int i = 0; i < order; i++) {
for(int j = 0; j < order; j++) {
if(i != 0 && j != exCol) { // When element is not in first row and exCol(excluded column) assign it to sub matrix
newArr[i-1][j > exCol ? j-1 : j] = prevArr[i][j]; // When column is greater than exCol subtract 1 from it
}
}
}
}
The recursion works perfectly and the problem is in the expression denoted above.
edit
for example if I input
2 -3 1
2 0 -1
1 4 5
while accessing the arr[0][0], arr[0][1], arr[1][0] and arr[1][1] in following sub-matrices
0 -1
4 5 --> in the first iteration of the array inside findDet()
2 -1
1 5 --> in the second iteration of the array inside findDet()
2 0
1 4 --> in the third iteration of the array inside findDet()
don't give me all the original values, they give some random values and some values that exist in the matrix.
you can check code comments for more details, I know this question is long and a bit confusing but I wish some of you can help me?
Can you please help me find the problem?
Thanks