int GetMinor(double **src, double **dest, int row, int col, int order)
{
int colCount=0,rowCount=0;
for(int i = 0; i < order; i++ )
{
if( i != row )
{
colCount = 0;
for(int j = 0; j < order; j++ )
{
if( j != col )
{
dest[rowCount][colCount] = src[i][j];
colCount++;
}
}
rowCount++;
}
}
return 1;
}
double Determinant( double **mat, int order)
{
if( order == 1 )
return mat[0][0];
double det = 0;
double **minor;
minor = new double*[order-1];
for(int i=0;i<order-1;i++)
minor[i] = new double[order-1];
for(int i = 0; i < order; i++ )
{
GetMinor( mat, minor, 0, i , order);
det += (i%2==1?-1.0:1.0) * mat[0][i] * Determinant(minor,order-1);
}
int main()
{
double S[3][3]={0.};
double C[3][3]={0.};
S[0][0]=1;
S[0][1]=1;
S[0][2]=2;
S[1][0]=S[0][1];
S[1][1]=3;
S[1][2]=1;
S[2][0]=S[0][2];
S[2][1]=S[1][2];
S[2][2]=2;
MatrixInv(S,3,C);
cout << "determinant of S = " << Determinant(S,3) << endl;
cin.get();
return 0;
}
The User defined functions are what I have found from some forums. But it does not work.
I get two errorw :
- error C2664: 'Determinant' : cannot convert parameter 1 from 'double [3][3]' to 'double **'
- error C2664: 'MatrixInv' : cannot convert parameter 1 from 'double [3][3]' to 'double **'
I have no idea what goes wrong. I appreciate if you could help me with it.
When they are passed by value to a function they are implicitly converted to pointers to their first elements. The element of a two dimensional array is one-dimensional array. So when you pass your arrays to a function they are converted to
But I still don't get it. How should I declare Matrices S, and C in the main program? Note that as they are passed to functions, their size will be different at each time. So I know they must be in a dynamic form. But I have no idea how to specify them at the main.
I did what you proposed. But i got the same error. the error is related to the time that the program wants to pass matrix to find its minor (adjugate) matrices. So, whenever I have "GetMinor(A,minor,j,i,order)" the error shows up.
I think I should not use matrix declaration in that way. I intend to calculate matrix inverse. Therefore, for a [3x3] matrix, it must also consider [2x2] matrices to bcalculate "adjugate" matrix for the initial one. Is there any other method I can specify the matrices S and C?