-
Notifications
You must be signed in to change notification settings - Fork 0
/
determinant.h
executable file
·70 lines (45 loc) · 1.29 KB
/
determinant.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef __LIB_DETERMINANT_
#define __LIB_DETERMINANT_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define SIZE 4
//int SIZE = 4;
void find_cofactor(double matrix[SIZE][SIZE], double submatrix[SIZE][SIZE], int subrow, int subcol, int subsize){
int i = 0;
int j = 0;
for (int row = 0; row < subsize; row++){
for (int col = 0; col < subsize; col++){
if (row != subrow && col != subcol){
submatrix[i][j++] = matrix[row][col];
if (j == subsize - 1){
j = 0;
i++;
}
}
}
}
}
double get_determinant(double matrix[SIZE][SIZE], int subsize){
double total = 0;
if (subsize == 1)
return matrix[0][0];
double submatrix[SIZE][SIZE];
int cofactor = 1;
for (int i = 0; i < subsize; i++){
find_cofactor(matrix, submatrix, 0, i, subsize);
total += cofactor * matrix[0][i] * get_determinant(submatrix, subsize - 1);
cofactor *= -1;
}
return total;
}
void print_table(double matrix[SIZE][SIZE]){
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++)
printf("%.1f\t", matrix[i][j]);
printf("\n");
}
}
#endif