Gaussian elimination code, where I am mistaken?

Hello there. So, for the last 2-2,5 hours I've been writing this code for the Gaussian elimination for solving matrix equations in the form Ax = b. Obviously, somewhere in my code, or algorithm, there's a logic error. I've commented on some important parts on the code to further explain what I am trying to do.

Thanks in advance.

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
  #include<iostream>
#include<cmath>
#include<fstream>

using namespace std;

ofstream fout ("output matrix gauss.txt");

double matrix[1000][1000];

//class for a position variable
typedef class{                    
public:
int i=0;
int j=0;
void create(int a, int b){
i = a;
j = b;
}
// (I've written some currently useless functions, left them in, I might need them)
void update(){                                          
i++;
j++;
}
void manual_update(int increase, string type){
if (type=="both"){
    i+=increase;
    j+=increase;
}
else if (type=="i"){
    i+=increase;
}
else if (type=="j"){
    j+=increase;
}
}
}Position;                                

void updateRow (int row, Position compare, int times, int dimension){
 //update row by substracting the correct stuff from another row
 for (int i=0; i<=dimension; i++){
   matrix[row][i] -= times * (matrix[compare.j][i]);
 }
}

int findTimes (Position current, Position compare){
// find times that the correct stuff from another row has got to be substracted (used with updateRow() )
int times = matrix[current.i][current.j] / matrix[compare.i][compare.j];
return times;
}

bool hasFinished (int i, int j){
//checks if it's on the main diagonal of the matrix and if it's 1, otherwise if it is zero
bool finished = false;
if (matrix[i][j]==1 && i==j){
    finished = true;
}
else if (matrix[i][j]==0 && i!=j){
    finished = true;
}
return finished;
}

int main(){
int n;
cin>>n;



for (int i=0; i<n; i++){          //reads the matrix 
    for (int j=0; j<n; j++){
        cin>>matrix[i][j];
    }
    cin>>matrix[i][n];
}
for (int j=0; j<n; j++){
    for (int i=0; i<n; i++){
        Position current;            //current position
        current.create(i, j);
        Position compare;           //position from where the updateRow() will be substracting
        if (i!=n-1){
            compare.create(i+1, j);
        }
        else{
            compare.create(i-1, j);
        }
        if (hasFinished (j, i)==false){
        updateRow(i, compare, findTimes(compare, current), n);  //checks whether it should update or not
        }
        for (int i=0; i<n; i++){
            for (int j=0; j<n+1; j++){
                fout<<matrix[i][j]<<" ";   //I outputed each step to a file to debug, never mind this
            }
            fout<<"\n";
        }
        fout<<"\n";
    }
}
for (int i=0; i<n; i++){
    cout<<matrix[i][n]<<"\n";           //Print result
}
}
Last edited on
Oh, I forgot to mention the input.

First line, an integer N, specifying number of variables and subsequently number of equations.
Then N lines, each consisting of N numbers representing each the coefficient of each variable and then another number representing the sum of the equation.
Topic archived. No new replies allowed.