Hello
I am trying to write a matrix calculator but I have some problems.I use cofactor method for calculation.If you don't know the this method you can learn it here easily: http://paulbourke.net/miscellaneous/determinant/
I use two function 1- GetMinor() 2- matrixCofactor() that the first one give me the minor matrix and I calculate determinant recursively in matrixCofactor() and print the determinant of the every matrix and its sub matrixes in every step. I don't know where the problem reside but I predict maybe problem be from recursive calls.I attach all my code file but I think this two function is enough for getting what is happening:
int GetMinor(Matrix *src,Matrix *dest, int row, int col, int order)
{
// indicate which col and row is being copied to dest
int colCount = 0,rowCount = 0;
int i,j;
for(i = 0; i < order; i++ ){
if( i != row )
{
colCount = 0;
for( j = 0; j < order; j++ )
{
// when j is not the element
if( j != col )
{
dest->elements[rowCount][colCount] = src->elements[i][j];
colCount++;
}
}
rowCount++;
}
}
return 0;
}
int matrixCofact(Matrix* mat,int order,double* determinan){
int i;
Matrix* minor;
minor = malloc(sizeof(double));
newMatrix(order-1,order-1,minor);
double* det;
det = malloc(sizeof(double));
if( order == 1 ){
printf("The End");
}
for(i = 0; i <order;i++){
GetMinor( mat, minor, 0, i, order);
printf("\n");
matrixPrint(minor);
if( minor->colCount > 3){
*det += pow(-1.0,i+0) * minor->elements[1][i] * matrixCofact(minor,order-1,det);
printf("\nThe matrix determinant is: %f",*det);
}else{
matrix3dDeterminan(minor,det);
printf("\nThe matrix determinant is: %f",*det);
}
}
printf("BYE ");
return 0;
}