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];
	    return 0;

	int matrixCofact(Matrix* mat,int order,double* determinan){
		int i;
		Matrix* minor;
		minor = malloc(sizeof(double));
		double* det;
		det = malloc(sizeof(double));
		if( order == 1 ){
			printf("The End");
		for(i = 0; i <order;i++){
			GetMinor( mat, minor, 0, i, order);
			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);
				printf("\nThe matrix determinant is: %f",*det);
		printf("BYE ");
		return 0;

Recommended Answers

All 3 Replies

I don't see anything obvious, but I'm not familiar with this, either.

If I were starting to debug it, I'd probably start with assertions to check that all the indices are staying within their proper ranges. Then start checking the subtotals, and whether j=0 is correct, for it's initialization (or should it be j=i?).
Should newmatrix be set to order-1?, etc.

There are lots of examples of this kind of code, but I don't know if it uses the cofactor method or not.

At line 42 you call matrixCofact as if it returns the minor's determinant. I recommend to (a) make matrixCofact return double, (b) get rid of its third parameter (you don't use it anyway), and (c) change line 50 to return det . Of course, det should be just double (not a pointer).
PS: minor->elements at line 52 is wrong. It should be mat->elements[0].
PPS: You probably know that it is one of the least efficient algorithms.

nezachem really thanks for your guide it was helpful. Another problem of my code is at line 41:

if( minor->colCount > 3)

it should be change to

if( order <3 )

because minor row and col amount is 1 less than order at every step because of this code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.