# Gauss elimination method for determinant and inverse matrix

### Title Introduction

Given a matrix of n*n, output its determinant value and inverse matrix.

### Explain

When reviewing linear algebra at the end of the term, I found that I always find the inverse matrix wrong, so I just wrote a program to realize it.. As for whether it's all or all or something Come and fill this hole later.
Gauss elimination method can solve linear equations with a little modification of the code, and it is not difficult to judge no solution / infinite solution. As for finding the free unknown quantity and outputting any solution I haven't thought about it yet. I'll fill it in later

```#include <bits/stdc++.h>
using namespace std;

const double eps = 1e-6;
const int N = 12;

void gauss(double a[][N], double b[], double x[], int n)
{
int i;
double s;
for (int k = 1; k <= n; ++k) {
for (i = k; i <= n && fabs(a[i][k]) < eps; ++i);
if (i != k) {
for (int j = k; j <= n; ++j)
swap(a[i][j], a[k][j]);
swap(b[i], b[k]);
}
for (i = k+1; i <= n; ++i) {
s = a[i][k] / a[k][k];
for (int j = k; j <= n; ++j)
a[i][j] -= a[k][j] * s;
b[i] -= b[k] * s;
}
}
for (i = n; i >= 1; --i) {
s = b[i];
for (int j = i+1; j <= n; ++j)
s -= x[j] *a[i][j];
x[i] = s / a[i][i];
if (fabs(x[i]) < eps) x[i] = 0;
}
}

int main()
{
double a[N][N], b[N], x[N], mt[N][N], mk[N][N];
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> mk[i][j];
a[i][j] = mk[i][j];
}
b[i] = 0;
}
gauss(a, b, x, n);
double det = 1;
for (int i = 1; i <= n; ++i)
det *= a[i][i];
if (fabs(det) < eps) det = 0;
printf("%0.2f\n", det);
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j)
a[i][j] = mk[i][j];
b[i] = 0;
}
b[k] = 1;
gauss(a, b, x, n);
for (int i = 1; i <= n; ++i)
mt[i][k] = x[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < n; ++j)
printf("%0.2f ", mt[i][j]);
printf("%0.2f\n", mt[i][n]);
}
return 0;
}```

Added by JimiLives on Sat, 02 May 2020 00:24:30 +0300