gaussian eliminaton c 2b 2b

Solutions on MaxInterview for gaussian eliminaton c 2b 2b by the best coders in the world

showing results for - "gaussian eliminaton c 2b 2b"
Dounia
25 Jul 2016
1#include <iostream>
2#include <cmath>
3#include <iomanip>
4using namespace std;
5
6#define maximumCount 1000
7
8int main() {
9    int n;
10    cout << "Enter the order of matrix:";
11    cin >> n;
12    float A[n][n+1];
13    float x[n];
14
15        for(int i=0;i<n;i++){
16        for(int j=0;j<n + 1;j++){
17        cout << "Enter matrix at a[" << i << "]" << "[" <<j << "]" << ": " ;
18        cin >> A[i][j];
19        }
20    }
21
22     //Upper triangular matrux
23    for(int j=0;j<n;j++){
24        for(int i=0;i<n;i++){
25            if(i>j){
26                float c = A[i][j] / A[j][j];
27                for(int k=0;k<n+1;k++){
28                    A[i][k] = A[i][k] - c*A[j][k];
29                }
30            }
31        }
32    }
33
34
35        //Dividing each row
36        for(int i=1;i<n;i++){
37            float c =  A[i][i];
38            for(int j=i;j<n+1;j++){
39                A[i][j] = A[i][j] / c;
40            }
41
42        }
43    for(int i=0;i<n;i++){
44        for(int j=0;j<n+1;j++){
45            cout << A[i][j] << " ";
46        }
47        cout << endl;
48    }
49
50
51
52        x[n-1] = A[n-1][n];
53
54        //backward substitution loop
55        for(int i=n-2;i>=0;i--){
56            float root = A[i][n];
57            for(int j = i+1; j<n ; j++){
58                root -= A[i][j] * x[j];
59            }
60            x[i] = root;
61        }
62        x[0] = x[0] / A[0][0];
63        for(int i=0; i<n; i++){
64            cout << x[i] << endl;;
65        }
66
67
68
69        return 0;
70}
71
72