Created
December 21, 2021 21:02
-
-
Save Yegorsh/5b35e7f1774fc513db59401aa5669f36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <ctime> | |
| #include <time.h> | |
| #define INTEGER | |
| //#define DECIMAL //you'll have to change the type of an array from int to float | |
| using namespace std; | |
| #ifdef INTEGER | |
| void FillMatrix(int **matrix, const int size){ | |
| for (int i=0; i<size; i++){ | |
| for (int j=0; j<size; j++){ | |
| cin >> matrix[i][j]; | |
| } | |
| } | |
| } | |
| #endif | |
| #ifdef DECIMAL | |
| void FillMatrix(float **matrix, const int size){ | |
| for (int i=0; i<size; i++){ | |
| for (int j=0; j<size; j++){ | |
| matrix[i][j]=static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/10)); //дробные числа от 0 до 10 | |
| } | |
| } | |
| } | |
| #endif | |
| void PrintMatrix(int **matrix, const int size){ | |
| for (int i=0; i<size; i++){ | |
| for (int j=0; j<size; j++){ | |
| cout << matrix[i][j] << "\t"; | |
| } | |
| cout << endl; | |
| } | |
| } | |
| void clearMemory(int** matrix, int size) { //Функция освобождающая память, выделенную под двумерный динамический массив | |
| for (int i = 0; i < size; i++) { | |
| delete[] matrix[i]; | |
| } | |
| delete [] matrix; | |
| } | |
| long long int NumberOfIterations = 0; | |
| int calcDet(int** matrix, int size){ | |
| int Det=0; | |
| if (size==1){ | |
| NumberOfIterations++; | |
| return matrix[0][0]; | |
| return NumberOfIterations; | |
| } | |
| else if (size==2){ | |
| NumberOfIterations++; | |
| return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]; | |
| return NumberOfIterations; | |
| } | |
| else{ | |
| int sign = 1; | |
| int** newMatrix = new int*[size-1]; | |
| for (int s=0; s<size-1; s++) { | |
| newMatrix[s] = new int[size-1]; | |
| } | |
| for (int k = 0; k < size; k++){ | |
| int sub_i = 0; | |
| for (int i = 1; i < size; i++){ | |
| int sub_j = 0; | |
| for (int j = 0; j < size; j++){ | |
| if (j == k){ | |
| continue; | |
| } | |
| else { | |
| newMatrix[sub_i][sub_j] = matrix[i][j]; | |
| sub_j++; | |
| NumberOfIterations++; | |
| } | |
| } | |
| sub_i++; | |
| } | |
| Det += sign * matrix[0][k] * calcDet(newMatrix,size-1); | |
| sign = -sign; | |
| } | |
| clearMemory(newMatrix,size-1); | |
| } | |
| return Det; | |
| return NumberOfIterations; | |
| } | |
| int main(){ | |
| srand(time(NULL)); | |
| clock_t start = clock(); | |
| int size; | |
| cout << "Размер матрицы:" << endl; | |
| cin >> size; | |
| int **matrix = new int *[size]; | |
| for (int i = 0; i < size; i++) { | |
| matrix[i] = new int [size]; | |
| } | |
| cout << "Введите элементы матрицы" << endl; | |
| FillMatrix(matrix,size); | |
| cout << "\nMatrix = " << endl; | |
| PrintMatrix(matrix,size); | |
| cout << endl << "Det(Matrix) = " << calcDet(matrix, size) << endl; | |
| clearMemory(matrix,size); | |
| clock_t end = clock(); | |
| double seconds = (double)(end - start) / CLOCKS_PER_SEC; | |
| cout << endl << "TIME = " << seconds << endl << "Количество итераций = " << NumberOfIterations; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment