I want to find the determinant of a square 4x4 matrix using a minor and cofactor.

And I want those in three seperate functions where i is the number of rows and j is the number of columns:

double minor(double a[][4], int row, int column);

Done by removing a row and column from the matrix. I know it's possible without pointers or classes, but I don't know how.

double cofactor(double a[][4], int row, int column);

The cofactor is the minor*(-1)^(i+j)
How does one do that exactly.

double det_c(double a[][4]

Specifically using a column so it goes this way:
-selecting a column
-multiply each element of that column by its cofactor
-Add the products obtained in the previous step


I do know how to make a matrix and print it on the screen:

#include <iostream>

using namespace std;

//Definieren van de maximale grootte van de matrix
#define MAX_SIZE 4

//Definieren van het type voor de matrix
typedef double matrix[MAX_SIZE][MAX_SIZE];	

//Functie prototype
void lees_matrix(matrix a, int k, int l);
void print_matrix(matrix c, int k,int j);
//double minor(matrix a, int rij, int kolom);
//double cofactor(matrix a, int i, int j);

int main()
{
    //Declaraties
    matrix a;
    int rij, kolom; 
                  
    cout <<"Voer het aantal rijen en kolommen voor de matrix in" << endl;
    cin >> rij >> kolom;
    
    if(rij>MAX_SIZE || kolom>MAX_SIZE)
	{
        cout << "De maximale grootte van de rijen of kolommen wordt overschreden." << endl;
        exit(1);
    }
    
    lees_matrix(a, rij, kolom);
    print_matrix(a, rij, kolom);
    minor(a, rij, kolom);
    
    system("PAUSE");
    return 0;
}



/*------------------------------------------------------------*/
//Function for reading matrix
void lees_matrix(matrix a, int k, int l)
{
	int rij, kolom;
	for (rij=0; rij<k; rij++)
	{
		cout << "Voer de data in voor rij "<< rij+1 << endl;
		for(kolom=0; kolom<l; kolom++)
		{
			cin >> a[rij][kolom];
		}
	}
	cout << endl;
    return;
}



/*------------------------------------------------------------*/
//Function for printing a matrix
void print_matrix(matrix a, int k,int j)
{
	int rij, kolom;
	for(rij=0; rij<k; rij++)
	{
		for(kolom=0; kolom<j; kolom++)
		{
			cout << a[rij][kolom] << " ";
		}
	    cout << endl;
    }
	return;  
}

I'm Dutch, so not everything might be clear, but I guess you can find out what I did here. (rij=row, kolom=column)

Ancient Dragon commented: Nicely written post and use of code tags :) +26

Recommended Answers

All 3 Replies

I have never done this math before so I would double check with a few test examples to see if this works and if my math is right.

All I did was went onto wiki and pretty much plugged the equations they give into functions.

Anyways here is what I came up with you get an idea of what is going on and finish off what you need to if you haven't already.

I have never done this math before so I would double check with a few test examples to see if this works and if my math is right.

All I did was went onto wiki and pretty much plugged the equations they give into functions.

Anyways here is what I came up with you get an idea of what is going on and finish off what you need to if you haven't already.

Thank you very much, I can work on it from here.

Just one more question.
For the determinant, is it possible to have a more general code that isn't specificalle for NxN matrices, but this

double determinant(double matrix[][3])
{
	/*
	         |a b c|
	matrix = |d e f|
	         |g h i|
	*/
	
	double aei = matrix[0][0]*matrix[1][1]*matrix[2][2];
	double afh = matrix[0][0]*matrix[1][2]*matrix[2][1];
	double bfg = matrix[0][1]*matrix[1][2]*matrix[2][0];
	double bdi = matrix[0][1]*matrix[1][0]*matrix[2][2];
	double cdh = matrix[0][2]*matrix[1][0]*matrix[2][1];
	double ceg = matrix[0][2]*matrix[1][1]*matrix[2][0];
		
	return (aei - afh) + (bfg - bdi) + (cdh - ceg);
}

seems to be specifically for this set.


And here you calculate the co-factor, but you aren't using it to calculate the determinant. Is that on purpose?

You could change the determinant() function to one that solves NxN. I chose to do the 3x3 because it is way less work but if you need or just want it to be NxN go for it.

As for your last question I'm not too sure what you are asking.

>>And here you calculate the co-factor, but you aren't using it to calculate the determinant. Is that on purpose?

I am not using what to calculate the determinant?

Be a part of the DaniWeb community

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