#include <iostream>
#include <ctime>
using namespace std;
/*
function determinant(m) {
if (m.length===1) {
return m[0][0];
}
if (m.length===2) {
return (m[0][0]*m[1][1]) - (m[0][1]*m[1][0]);
} else {
let BigArr = [];
for (let t=0; t<m[0].length; t++) {
let smallArr = [];
for(let r=1; r<m.length; r++) {------------------------------
let arr = [];
for(let e=0; e<m[0].length; e++) {
if (e!=t) {
arr.push(m[r][e]);
}
}
smallArr.push(arr);
}
BigArr.push(determinant(smallArr));
}
let tmp = true;
let current = 0;
for (let t=0; t<m.length; t++) {
(tmp) ? current+=m[0][t]*BigArr[t] : current-=m[0][t]*BigArr[t];
tmp=!tmp;
}
return current;
}
};
*/
int determinant(int** m,const int length) {
if (length == 1) {
return m[0][0];
}
if (length == 2) {
return (m[0][0] * m[1][1]) - (m[0][1] * m[1][0]);
}
else {
int* BigM = new int[length];
int BigLength = 0;
for (int t = 0; t < length; t++) {
int** Buf = new int* [length];
for (int i = 0; i < length - 1; i++) {
Buf[i] = new int[length - 1];
}
// массив создан
int bufLength = 0;
for (int i = 1; i < length; i++) {
int shet = 0;
for (int w = 0; w < length; w++) {
if (w!=t) {
Buf[bufLength][shet] = m[i][w];
shet++;
}
}
bufLength++;
}
BigM[BigLength] = determinant(Buf, length - 1);
BigLength++;
}
bool tmp = true;
int current = 0;
for (int t = 0; t < length; t++) {
(tmp) ? current += m[0][t] * BigM[t] : current -= m[0][t] * BigM[t];
tmp = !tmp;
}
return current;
}
}
int** CoutArr(int** Array,int size)
{
for (int i = 0; i < size; i++) {
for (int r = 0; r < size; r++) {
cout << Array[i][r] << "\t";
}
cout << '\n';
}
return Array;
}
int main()
{
/*
[[2,4,2],[3,1,1],[1,2,0]]
*/
setlocale(LC_ALL,"Russian");
const int size = 11;
unsigned int start_time = clock(); // начальное время
int arr[size][size];
for (int t = 0; t < size; t++) {
for (int r = 0; r < size; r++) {
arr[t][r] = rand() % 100;
}
}
int *p[size];
for (int i = 0; i < size; i++) {
p[i] = arr[i];
}
CoutArr(p,size);
int d = determinant(p, size);
cout << "\n" << d;
unsigned int end_time = clock(); // конечное время
unsigned int search_time = end_time - start_time;
cout << "\n\n" << search_time << "сек";
}