File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* Gauss elimination method */
2
+ #include < iostream>
3
+ #include < iomanip>
4
+ #include < math.h>
5
+ #define N 4
6
+ using namespace std ;
7
+ int main (){
8
+ float a[N][N+1 ],x[N],t,s;
9
+ int i,j,k;
10
+ cout << " Enter the elements of the"
11
+ " augmented matrix rowwise" << endl;
12
+ cout << fixed;
13
+ for (i=0 ;i<N;i++)
14
+ for (j=0 ;j<N+1 ;j++)
15
+ cin >> a[i][j]);
16
+ for (j=0 ;j<N-1 ;j++)
17
+ for (i=j+1 ;i<N;i++){
18
+ t = a[i][j]/a[j][j];
19
+ for (k=0 ;k<N+1 ;k++)
20
+ a[i][k] -= a[j][k]*t;
21
+ }
22
+ /* now printing the
23
+ upper triangular matrix */
24
+ cout << " The upper triangular matrix"
25
+ " is:-" << endl;
26
+ for (i=0 ;i<N;i++){
27
+ for (j=0 ;j<N+1 ;j++)
28
+ cout << setw (8 ) << setprecision (4 ) << a[i][j];
29
+ cout << endl;
30
+ }
31
+ /* now performing back substitution */
32
+ for (i=N-1 ;i>=0 ;i--){
33
+ s = 0 ;
34
+ for (j=i+1 ;j<N;j++)
35
+ s += a[i][j]*x[j];
36
+ x[i] = (a[i][N]-s)/a[i][i];
37
+ }
38
+ /* now printing the results */
39
+ cout << " The solution is:- " << endl;
40
+ for (i=0 ;i<N;i++)
41
+ cout << " x[" << setw (3 ) << i+1 << " ] = "
42
+ << setw (7 ) << setprecision (4 ) << x[i] << endl;
43
+ return 0 ;
44
+ }
You can’t perform that action at this time.
0 commit comments