forked from CodeX-SIT/cpp-algorithm-design-manyagupta0209
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineareq.cpp
80 lines (73 loc) · 1.69 KB
/
lineareq.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
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
#define SIZE 10
using namespace std;
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n,c=0;
/* Setting precision and writing floating point values in fixed-point notation. */
cout<< setprecision(3)<< fixed;
/* Inputs */
/* 1. Reading number of unknowns */
cout<<"Enter number of unknowns: ";
cin>>n;
/* 2. Reading Augmented Matrix */
cout<<"Enter Coefficients of Augmented Matrix: "<< endl;
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
cout<<"a["<< i<<"]"<< j<<"]= ";
cin>>a[i][j];
}
}
/* Applying Gauss Jordan Elimination */
for(i=0;i<n;i++)
{
if (a[i][i] == 0)
{
c=1;
while(c<n-i)
{
if (a[i+c][i] == 0)
c++;
else
break;
}
if (c==n-i)
{
cout<<"NO SOLUTION";
return 0;
}
for(int k=0;k<n+1;k++)
{
int swap = a[i][k];
a[i][k] = a[i+c][k];
a[i+c][k] = swap;
}
}
for(j=0;j<n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=0;k<n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
/* Obtaining Solution */
for(i=0;i<n;i++)
{
x[i] = a[i][n]/a[i][i];
}
/* Displaying Solution */
cout<< endl<<"Solution: "<< endl;
for(i=0;i<n;i++)
{
cout<<"x["<< i<<"] = "<< x[i]<< endl;
}
return(0);
}