-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gauss Inverse.cpp
73 lines (59 loc) · 1.56 KB
/
Gauss Inverse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cout << "Enter number of variable:";
cin >> n;
float A[2*n][2*n], temp;
//input system
cout << "enter coeff of eqn:" << endl;
for(int i = 0; i < n ; i++)
for(int j = 0; j < (n+1) ; j++) //n+1 for augmented constant
cin >> A[i][j];
//for addting unit mat at the end
for(int i = 0; i < n ; i++)
{
for(int j = n; j < 2*n ; j++)
{
if((i+n) == j)
A[i][j] = 1;
else
A[i][j] = 0;
}
}
//column action
for(int j = 0; j < n ; j++)
{
//row acion
for(int i = 0; i < n ;i++)
{
if(i != j) //making other element other than diagonal as zero
{
temp = A[i][j]/A[j][j];
for(int k = 0; k < (2*n) ; k++) //till 2nth element
{
A[i][k] = A[i][k] - temp*A[j][k];
}
}
else //making diagonal element 1
{
temp = A[i][j]; //a11 or a22 or a33 ...value
for(int k = 0; k < (2*n) ; k++)
{
A[i][k] = A[i][k]/temp;
}
}
}
}
cout << "Inverse mat is:" << endl;
for(int i = 0; i < n ; i++)
{
for(int j = n; j < (2*n); j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
}