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;
}

/*------------------------------------------------------------*/
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)

Nicely written post and use of code tags :)
2
Contributors
3
Replies
5
Views
8 Years
Discussion Span
Last Post by sfuo

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.

Attachments
``````#include <iostream>

using namespace std;

double power(int base, int exp);
double determinant(double matrix[0][3]);
double minor(double matrix[0][4], int row, int col);
double cofactor(double matrix[0][4], int row, int col);

int main()
{
double matrix[4][4];

matrix[0][0] = -2;
matrix[0][1] = 2;
matrix[0][2] = -3;
matrix[0][3] = 0;
matrix[1][0] = -1;
matrix[1][1] = 1;
matrix[1][2] = 3;
matrix[1][3] = 0;
matrix[2][0] = 2;
matrix[2][1] = 0;
matrix[2][2] = -1;
matrix[2][3] = 0;
matrix[3][0] = 0;
matrix[3][1] = 0;
matrix[3][2] = 0;
matrix[3][3] = 0;

cout << cofactor(matrix, 3, 3) << endl;

system("PAUSE");
return 0;
}

//For the power function you can just use the one in the math header
//I made this one really quick
double power(int base, int exp)
{
double result = base;

if( exp < 0 )
return 0; //cant do it

if( exp == 0 )
return 1;

if( exp == 1 )
return result;

for( int i = 1; i <= exp; i++ )
result *= base;

return result;
}

//returns the determinant of a [3][3] matrix
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);
}

//changes the [4][4] matrix into a [3][3] based off row and col
//then outputs the determinant of the [3][3] matrix
double minor(double matrix[][4], int row, int col)
{
double _matrix[3][3];
int _row = 0, _col = 0;

for( int i = 0; i < 4; i++ )
{
if( i != row ) //ignores the selected row
{
for( int j = 0; j < 4; j++ )
{
if( j != col ) //ignores the selected col
{
_matrix[_row][_col] = matrix[i][j];
_col++;
}
}
_col = 0;
_row++;
}
if( _row > 2)
_row = 0;
}

return determinant(_matrix);
}

//returns minor*(-1)^(row+col)
double cofactor(double matrix[0][4], int row, int col)
{
return minor(matrix, row, col) * power(-1, row+col);
}``````

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.