#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
using namespace std;
int const N = 4;
double Gauss_det(double X[N][N])
{
double c;
for (int i = 0; i<N-1; i++){
for(int j = 1 + i; j < N; j++){
c = X[j][i] / X[i][i];
for (int f = i; f < N; f++){
X[j][f] -= c * X[i][f];
}
}
}
c = 1;
for (int i=0; i < N; i++)
c*=X[i][i];
return c;
}
void Inverse_matrix(double X[N][N]){
double M[N][N] = {{1.0, 0.0, 0.0, 0.0},{0.0, 1.0, 0.0, 0.0},{0.0, 0.0, 1.0, 0.0},{0.0, 0.0, 0.0, 1.0}};
double c;
for (int i = 0; i<N-1; i++){
for(int j = 1 + i; j < N; j++){
c = X[j][i] / X[i][i];
for (int f = i; f < N; f++){
X[j][f] -= c * X[i][f];
}
for (int f = 0; f < N; f++){
M[j][f] -= c * M[i][f];
}
}
}
for (int i = N-1; i>0; i--){
for(int j = i -1; j > -1; j--){
c = X[j][i] / X[i][i];
for (int f = i; f > -1; f--){
X[j][f] -= c * X[i][f];
}
for (int f = N; f > -1; f--){
M[j][f] -= c * M[i][f];
}
}
}
for (int i=0; i<N; i++){
for (int j =0; j< N; j++){
M[i][j] /= X[i][i];
}
}
for (int i=0; i<N; i++){
for (int j =0; j< N; j++){
X[i][j] = M[i][j];
}
}
}
void Output_x(double const X[N][N]){
for (int i=0; i<N; i++){
for (int j =0; j< N; j++){
cout << fixed << setprecision(6) << X[i][j] << "\t" ;
}
cout << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "rus");
double X[N][N] = {{1,0.42,0.54,0.66},{0.42,1,0.32,0.44},{0.54,0.32,1,0.22},{0.66,0.44,0.22,1}};
cout << "Нахождение определителя методом Гаусса" << endl << endl << "Исходная матрица" << endl;
Output_x(X);
cout << "Определитель равен: " << Gauss_det(X) << endl << endl;
double X1[N][N] = {{1.0, 0.47, -0.11, 0.55},{0.42, 1.0, 0.35, 0.17},{-0.25, 0.67, 1.0, 0.36},{0.54, -0.32, -0.74, 1.0}} ;
cout << "Нахождение обратной матрицы методом Гаусса" << endl << endl << "Исходная матрица" << endl;
Output_x(X1);
cout << endl << "Обратная матрица" << endl;
Inverse_matrix(X1);
Output_x(X1);
cout << endl;
system ("pause");
return 0;
}
3.