Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<stdlib.h>
- #include<time.h>
- #include<Windows.h>
- #include <iomanip>
- using namespace std;
- //алгебраическое дополнение к элементу a[row][col] генерация их
- double add(double a[N][M], int row, int col)
- {
- double b[2][2];
- int i, j, bi, bj;
- for (i = 0, bi = 0; i<3; i++)
- {
- if (i != row)
- {
- for (j = 0, bj = 0; j<3; j++)
- if (j != col)
- {
- b[bi][bj] = a[i][j];
- bj++;
- }
- bi++;
- }
- }
- if ((row + col) % 2)
- return b[0][1] * b[1][0] - b[0][0] * b[1][1];
- else
- return b[0][0] * b[1][1] - b[0][1] * b[1][0];
- }
- //определитель матрицы
- double det(double a[N][M])
- {
- int i;
- double sum;
- for (i = 0, sum = 0.; i<3; i++)
- sum += a[i][0] * add(a, i, 0);
- if (sum < 0)
- cout << "ERROR DETERMINANT NOL'";
- return sum;
- }
- //обратная матрица
- void inverse(double a[N][M], double d)
- {
- double a1[3][3], cur;
- int i, j;
- for (i = 0; i < 3; i++)
- for (j = 0; j < 3; j++)
- a1[i][j] = add(a, i, j) / d;
- for (i = 0; i < 3; i++)
- for (j = i + 1; j < 3; j++)
- {
- cur = a1[i][j];
- a1[i][j] = a1[j][i];
- a1[j][i] = cur;
- }
- if (d < 0)
- cout << "Error n";
- else {
- cout << "nobratnya matrix :n";
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 3; j++)
- cout << setw(7) << a1[i][j];
- cout << "n";
- }
- }
- }
- //основная функция
- int main()
- {
- int N, M; //размерность матрицы
- double determinant; // объявление детерминанта(опеределителя)
- int i, j;
- int ** arr = new int *[N];
- cout << "n "; cin >> N;
- cout << "m "; cin >> M;
- for (i = 0; i<N; i++)
- int** a = new int *[M];
- for (j = 0; j<M; j++)
- arr[i][j] = rand() % 10 - 5; //задаем рандом в диапазоне от +10 до -5
- cout << "matrix A:n";
- for (i = 0; i<3; i++)
- {
- for (j = 0; j<3; j++)
- cout << setw(7) << setprecision(2) << arr[i][j]; //setprecision - сокращение до двух знаков после запятой //setw(7) расстояние между знаками
- cout << "n";
- }
- determinant = det(arr);
- cout << "ndeterminant: " << determinant << "n"; //вывод детерминанта
- if (determinant) inverse(arr, determinant); //в другом случае
- else cout << "obrantoy matrici net";
- getchar();
- return 0;
- }
- double add(double a[N][M], unsigned row, unsigned col, unsigned N, unsigned M)
- {
- double b[2][2];
- unsigned i, j, bi, bj;
- for (i = 0, bi = 0; i<N; i++)
- {
- if (i != row)
- {
- for (j = 0, bj = 0; j<M; j++)
- if (j != col)
- {
- b[bi][bj] = a[i][j];
- bj++;
- }
- bi++;
- }
- }
- if ((row + col) % 2)
- return b[0][1] * b[1][0] - b[0][0] * b[1][1];
- else
- return b[0][0] * b[1][1] - b[0][1] * b[1][0];
- }
- //определитель матрицы
- double det(double a[N][M], unsigned N, unsigned M)
- {
- int i;
- double sum;
- for (i = 0, sum = 0.; i<N; i++)
- sum += a[i][0] * add(a, i, 0, N, M);
- if (sum < 0)
- cout << "ERROR DETERMINANT NOL'";
- return sum;
- }
- //обратная матрица
- void inverse(double a[N][M], double d, unsigned N, unsigned M)
- {
- double a1[N][M], cur;
- unsigned i, j;
- for (i = 0; i < N; i++)
- for (j = 0; j < M; j++)
- a1[i][j] = add(a, i, j, N, M) / d;
- for (i = 0; i < N; i++)
- for (j = i + 1; j < M; j++)
- {
- cur = a1[i][j];
- a1[i][j] = a1[j][i];
- a1[j][i] = cur;
- }
- if (d < 0)
- cout << "Error n";
- else {
- cout << "nobratnya matrix :n";
- for (i = 0; i <N; i++)
- {
- for (j = 0; j < M; j++)
- cout << setw(7) << a1[i][j];
- cout << "n";
- }
- }
- }
- //основная функция
- int main()
- {
- unsigned N, M; //размерность матрицы
- double determinant; // объявление детерминанта(опеределителя)
- unsigned i, j;
- cout << "n "; cin >> N;
- cout << "m "; cin >> M;
- int ** arr = new int*[N];
- for (i = 0; i<N; i++)
- arr[i] = new int[M];
- for (j = 0; j<M; j++)
- arr[i][j] = rand() % 10 - 5; //задаем рандом в диапазоне от +10 до -5
- cout << "matrix A:n";
- for (i = 0; i<N; i++)
- {
- for (j = 0; j<M; j++)
- cout << setw(7) << setprecision(2) << arr[i][j]; //setprecision - сокращение до двух знаков после запятой //setw(7) расстояние между знаками
- cout << "n";
- }
- determinant = det(arr, N, M);
- cout << "ndeterminant: " << determinant << "n"; //вывод детерминанта
- if (determinant) inverse(arr, determinant, N, M); //в другом случае
- else cout << "obrantoy matrici net";
- getchar();
- return 0;
- }
Add Comment
Please, Sign In to add comment