코딩 가이드에 맞추느라 재해석이 불가함 ;;;
뭐 어쨌든... NxN 배열을 포인터로 넘기고, 그 배열의 크기를 넘기면
double형의 Det(A)값이 return 됨.
1 by 1 까지 내려가지 않고 2by2 에서는 ad-bc의 값이 리턴 되어 재귀호출로 값을 구함.
닫기 Code Type : C
static double
Determinant (double** matrixA, int userN)
{
int userM = 0;
int counter = 0;
int determinantACheck = 0;
int determinantARow = 0;
int determinantAColumn = 0;
int sign = 1;
double determinantResult = 0;
double** matrixDeterminantA;
userM = userN - 1;
matrixDeterminantA = (double**)malloc(sizeof(double*) * userM);
for (counter = 0; counter < userM; counter++)
{
matrixDeterminantA[counter] = (double*)malloc(sizeof(double) * userM);
} /* for (counter = 0; counter < userM; counter++) */
if (userN == 2)
{
determinantResult = (matrixA[0][0] * matrixA[1][1]) -
(matrixA[0][1] * matrixA[1][0]);
} /* if (userN == 2) */
else
{
for (counter = 0; counter < userN; counter++)
{
for (determinantARow = 0; determinantARow < userM;
determinantARow++)
{
determinantACheck = 0;
for (determinantAColumn = 0; determinantAColumn < userM;
determinantAColumn++)
{
if (determinantAColumn == counter)
{
determinantACheck++;
} /* if (determinantAColumn == counter) */
matrixDeterminantA[determinantARow][determinantAColumn] =
matrixA[determinantARow + 1][determinantACheck];
determinantACheck++;
} /* for (determinantAColumn = 0; determinantAColumn < userM;
determinantAColumn++) */
} /* for (determinantARow = 0; determinantARow < userM;
determinantARow++) */
if(counter % 2 == 1)
{
sign = -1;
} /* if(counter % 2 == 1) */
else
{
sign = 1;
} /* else */
determinantResult += sign * matrixA[0][counter] *
Determinant(matrixDeterminantA,userM);
} /* for (counter = 0; counter < userN; counter++) */
} /* else */
for(counter = 0; counter < userM; counter++)
{
free(matrixDeterminantA[counter]);
} /* for(counter = 0; counter < userM; counter++) */
free(matrixDeterminantA);
return determinantResult;
}
닫기