Yêu cầu: Tính định thức của ma trận NxN
Giải thuật: Định thức của ma trận vuông được tính theo công thức sau:
Định thức cấp 2:
Định thức cấp 3:
…
Việc tính
- Định thức cấp N dựa vào việc tính định thức cấp (N-1).
- Định thức cấp (N-1) dựa vào việc tính định thức cấp (N-2).
- …
- Định thức cấp 4 dựa vào việc tính định thức cấp 3.
- Định thức cấp 3 dựa vào việc tính định thức cấp 2.
Do vậy, chúng ta sử dụng đệ quy để tính định thức cấp N
/************************Determinate of matrix******************/
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define N 10
#define SIGN(x) (x%2? -1 : 1)
int** allocArr(short szRow, short szCol);
void ImportData(int** Arr, int nRow, int nCol, char name);
int DetOfMatrix(int** Arr, int sz);
void FreeMem(int **Buff, int n);
void main()
{
int iRow, iCol;
int size;
int** Arr;
printf("\nsize of matrix: ");
scanf("%d", &size);
Arr = allocArr(size, size);
ImportData(Arr, size, size, 'A');
printf("det(A) = %d", DetOfMatrix(Arr, size));
getch();
}
/*************************************************************
//Function : Import data for matrix
//Parameter: matrix, and the number of row and column and
//name matrix
//Return : void
*************************************************************/
void ImportData(int** Arr, int nRow, int nCol, char name)
{
int iRow, iCol;
for(iRow = 0; iRow < nRow; iRow++)
for(iCol = 0; iCol < nCol; iCol++)
{
printf("\n%c[%d][%d] = ", name, iRow, iCol);
scanf("%d", &Arr[iRow][iCol]);
}
}
/************************************************************
*Function : calculate determinant of matrix nxn
*Parameter: matrix, and the number of row and column
*Return : void
************************************************************/
int DetOfMatrix(int** Arr, int sz)
{
int i, j, k, p = 0;
int det = 0;
int m, n;
int** subArr;
if (sz < 2)
{
return -1;
}
if (sz == 2)
{
return (Arr[0][0]*Arr[1][1] - Arr[0][1]*Arr[1][0]);
}
else
{
if (!(subArr = allocArr(sz - 1, sz - 1)))
{
return -1;
}
for (i = 0; i < sz; i++)
{
m = n = 0;
for (j = 1; j < sz; j++)
{
for (k = 0; k < sz; k++)
{
if (k != i)
{
subArr[m][n] = Arr[j][k];
n++;
if (n == sz-1)
{
m++;
n = 0;
}
}
}
}
det += SIGN(i)*Arr[0][i]*DetOfMatrix(subArr, sz - 1);
}
FreeMem(subArr, sz-1);
return det;
}
}
int** allocArr(short szRow, short szCol)
{
int i;
int **pArr;
// Allocate memory for array
pArr = (int**)malloc(szRow * sizeof(int *));
if (!pArr)
{
return NULL;
}
for(i = 0; i < szRow; i++)
{
pArr[i] = (int*)malloc(szCol * sizeof(int));
if (!pArr[i])
{
return NULL;
}
}
return pArr;
}
/*************************************************************
*Function : free allocated memory
*Parameter: pointer to memory
*Return : void
*************************************************************/
void FreeMem(int **Buff, int n)
{
int i, j;
for (i = 0; i < n; i++)
{
if (Buff[i])
{
free(Buff[i]);
Buff[i] = NULL;
}
}
if (Buff)
{
free(Buff);
}
}
Kết quả:
1 2 3 4 5 6 7 8 9 10 11 |
size of matrix: 3 A[0][0] = -3 A[0][1] = 4 A[0][2] = 11 A[1][0] = 22 A[1][1] = 9 A[1][2] = 7 A[2][0] = 5 A[2][1] = 3 A[2][2] = -4 det(A) = 894 |
Leave a Reply