A Gauss-Jordan C++ Code

Hi there. This is a simple Gauss-Jordan Elimination matrix code. I just want to ask for comments with this code since I'm a beginner. Thank you. Any comment is welcome.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*************** Gauss Jordan Elimination Matrix ********************/
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;

int i,j,k,n;
float a[10][11]={0},d;

int main()
{
	system("CLS");
	cout << " ==============================================================================" << endl;
	cout << " ======================== Gauss-Jordan Emilimination Matrix ===================" << endl;
	cout << " ==============================================================================" << endl;
	cout << endl;
	cout << "How many variables? ";
	cin >> n;
	cout << endl;
    cout << "Please enter the coefficients:" << endl;
    for(i = 0; i < n; i++)			//Inputs the coefficients of the matrix
    {
		for(j = 0; j < n+1; j++)
        {
			cin >> a[i][j];
		}
	}
            
    cout << endl;
    
	cout << "This is your input:" << endl;
	
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n+1; j++)
		{
			cout << "" << a[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
	system("pause");
	
    /************** Partial pivoting **************/
    
	for(i = 0; i < n; i++)
    {
		for(j = 0; j < 2*n; j++)
        {
		    if(j == (i+n))
                a[i][j] = 1;
		}
	}
	
    for(i = n; i > 1; i--)
    {
        if(a[i-1][1] < a[i][1])
        for(j = 0; j < 2*n+1; j++)
        {
            d = a[i][j];
            a[i][j] = a[i-1][j];
            a[i-1][j] = d;
        }
    }
    cout << endl;
    
    cout << "Pivoted output: " << endl;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < 2*n+1; j++)
        {
              cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    /********** Reducing To Diagonal Matrix ***********/

    for(i = 0; i < n; i++)
    {
        for(j = 0; j < 2*n+1; j++)
        if(j!=i)
        {
            d=a[j][i]/a[i][i];
            for(k = 0; k < n*2; k++)
                a[j][k] -= a[i][k]*d;
        }
    }
    cout << endl;
    /************** Reducing To Unit Matrix *************/
    for(i = 0; i < n; i++)
    {
    	d = a[i][i];
        for(j = 0; j < 2*n+1; j++)
            a[i][j] = a[i][j]/d;
    }


    cout<<"Your solutions: "<<endl;
    for(i=0; i < n; i++)
    {
        for(j = n+1; j < 2*n+1; j++)
            cout << a[i][j] <<"    ";
        cout << endl;
    }
	system ("pause");
    return 0;
}
Topic archived. No new replies allowed.