#include <stdio.h>
#define MATRIX double**
MATRIX CreateMatrix(int rows, int cols)
{
int i = 0;
MATRIX result = malloc(rows * sizeof(double*));
for (; i < rows; i++ ) result[i] = malloc(cols * sizeof(double));
return result;
}
void FreeMatrix(MATRIX matrix, int rows)
{
for (; rows; rows--) free(matrix[rows-1]);
free(matrix);
}
MATRIX CreateMinor(MATRIX source, int size, int row, int col)
{
MATRIX m = CreateMatrix(size - 1, size - 1);
int i = 0, j;
for (; i < size - 1; i++)
for (j = 0; j < size - 1; j++)
m[i][j] = source[i + (i < row ? 0 : 1)][j + (j < col ? 0 : 1)];
return m;
}
double Det(MATRIX matrix, int size);
double Det(MATRIX matrix, int size)
{
if (size == 1) return matrix[0][0];
int i;
double result = 0.0;
for (i = 0; i < size; i++)
{
MATRIX minor = CreateMinor(matrix, size, 0, i);
double d = Det(minor, size - 1);
result += (i % 2 ? 1 : -1) * d;
FreeMatrix(minor, size - 1);
}
return result;
}
MATRIX ReadMatrix(int cols, int rows)
{
MATRIX m = CreateMatrix(cols, rows);
int i = 0, j;
for (; i < rows; i++)
for(j = 0; j < cols; j++) scanf("%lf", &m[i][j]);
return m;
}