# C++ Program to Implement Gauss Jordan Elimination

«
»
This is a C++ Program to implement Gauss Jordan Elimination algorithm. In linear algebra, Gaussian elimination (also known as row reduction) is an algorithm for solving systems of linear equations. It is usually understood as a sequence of operations performed on the associated matrix of coefficients. This method can also be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix.

Here is source code of the C++ Program to Implement Gauss Jordan Elimination. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*************** Gauss Jordan method for inverse matrix ********************/`
2. `#include<iostream>`
3. `#include<conio.h>`
4. ` `
5. `using namespace std;`
6. ` `
7. `int main()`
8. `{`
9. `    int i, j, k, n;`
10. `    float a = { 0 }, d;`
11. `    cout << "No of equations ? ";`
12. `    cin >> n;`
13. `    cout << "Read all coefficients of matrix with b matrix too " << endl;`
14. `    for (i = 1; i <= n; i++)`
15. `        for (j = 1; j <= n; j++)`
16. `            cin >> a[i][j];`
17. ` `
18. `    for (i = 1; i <= n; i++)`
19. `        for (j = 1; j <= 2 * n; j++)`
20. `            if (j == (i + n))`
21. `                a[i][j] = 1;`
22. ` `
23. `    /************** partial pivoting **************/`
24. `    for (i = n; i > 1; i--)`
25. `    {`
26. `        if (a[i - 1] < a[i])`
27. `            for (j = 1; j <= n * 2; j++)`
28. `            {`
29. `                d = a[i][j];`
30. `                a[i][j] = a[i - 1][j];`
31. `                a[i - 1][j] = d;`
32. `            }`
33. `    }`
34. `    cout << "pivoted output: " << endl;`
35. `    for (i = 1; i <= n; i++)`
36. `    {`
37. `        for (j = 1; j <= n * 2; j++)`
38. `            cout << a[i][j] << "    ";`
39. `        cout << endl;`
40. `    }`
41. `    /********** reducing to diagonal  matrix ***********/`
42. ` `
43. `    for (i = 1; i <= n; i++)`
44. `    {`
45. `        for (j = 1; j <= n * 2; j++)`
46. `            if (j != i)`
47. `            {`
48. `                d = a[j][i] / a[i][i];`
49. `                for (k = 1; k <= n * 2; k++)`
50. `                    a[j][k] -= a[i][k] * d;`
51. `            }`
52. `    }`
53. `    /************** reducing to unit matrix *************/`
54. `    for (i = 1; i <= n; i++)`
55. `    {`
56. `        d = a[i][i];`
57. `        for (j = 1; j <= n * 2; j++)`
58. `            a[i][j] = a[i][j] / d;`
59. `    }`
60. ` `
61. `    cout << "your solutions: " << endl;`
62. `    for (i = 1; i <= n; i++)`
63. `    {`
64. `        for (j = n + 1; j <= n * 2; j++)`
65. `            cout << a[i][j] << "    ";`
66. `        cout << endl;`
67. `    }`
68. ` `
69. `    getch();`
70. `    return 0;`
71. `}`

Output:

```\$ g++ GaussJordanElimination.cpp
\$ a.out

No of equations ? 3
Read all coefficients of matrix with b matrix too
2 3 4
5 6 3
9 8 6
pivoted output:
9    8    6    0    0    1
2    3    4    1    0    0
5    6    3    0    1    0
-0.292683    -0.341463    0.365854
0.0731707    0.585366    -0.341463
0.341463    -0.268293    0.0731708```

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 