include iostream include cmath using namespace std double determinant

 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
#include <iostream>
#include <cmath>
using namespace std;
double determinant(int **array, size_t n) {
double det = 0;
int **temp = new int *[n];
for (int i = 0; i < n; i++) {
temp[i] = new int[n];
}
if (n == 2) {
det = ((array[0][0] * array[1][1]) - (array[1][0] * array[0][1]));
} else {
for (int x = 0; x < n; x++) {
int temp_i = 0;
for (int i = 1; i < n; i++) {
int temp_j = 0;
for (int j = 0; j < n; j++) {
if (j == x) {
continue;
}
temp[temp_i][temp_j] = array[i][j];
temp_j++;
}
temp_i++;
}
det = det + (pow(-1, x) * array[0][x] * determinant(temp, n - 1));
}
}
return det;
}
// Основная функция
int main() {
size_t n;
double determ;
cout << "enter dimension: ";
cin >> n;
int **arr = new int *[n];
for (int i = 0; i < n; i++) {
arr[i] = new int[n];
for (int j = 0; j < n; j++) {
cout << "mas[" << i << "][" << j << "]=";
cin >> arr[i][j];
}
}
determ = determinant(arr, n);
cout << "determ = " << determ << endl;
return 0;
}