Gaussian elimination [repost]

(Note: this is a repost from a post I posted 3 days ago but it got no replies, so I am reposting it. I hope I don't get banned)

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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, double times, int dimension, string type){
if (type=="else"){
 for (int i=0; i<=dimension; i++){
   matrix[row][i] -= times * (matrix[compare.j][i]);
 }
}
else if (type=="main"){
    for (int i=0; i<=dimension; i++){
        matrix[row][i] *= times;
    }
}
}

double findTimes (Position current, Position compare, string type){
double times;
    if (type=="main"){
  times = 1 / matrix[current.i][current.j];
    }
else if (type=="else"){
    times = matrix[current.i][current.j] / matrix[compare.i][compare.j];
}
return times;
}

bool hasFinished (int i, int j){
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 && i==j){
        updateRow(i, compare, findTimes(compare, current, "main"), n, "main");  //checks whether it should update or not
        }
        else if (hasFinished(j, i)==false && i!=j){
            updateRow(i, compare, findTimes(compare, current, "else"), n, "else");
        }
        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
Obviously, somewhere in my code, or algorithm, there's a logic error.

Why is it obvious?

I suggest that you throughly describe your problem. Then show what you inputted into the program, what the program outputted with that input and what you expected the output to be.
It is obvious because if there wasn't I wouldn't be posting this here.

As the problem description, well I forgot to mention that. It is a program to solve equation systems with equations in the form Ax = b using Gaussian Elimination. You are given an integer N, specifying the number of variables, and therefore, number of equations. Then N lines, each with N integers, specifying each the coeffiecient of each variable and another integer specifying the sum of those variables.

Output should be the value of each variable.

Example for (x=1, y=2, z=3)
Input:
3
1 1 1 6
1 1 2 9
1 2 1 8

Output:
1
2
3

My program for the same input outputs:
2
-14,5
8
Last edited on
Your matrix is double but input is all int. Would ints suffice?
I put double because some row operations might return a decimal value at some point. I've changed a bit the code, I will now edit the post to the new one. (It still returns wrong ouput though)
The program should, at the end, after all the row operations modify the matrix like this(for the example above):

1 0 0 1
0 1 0 2
0 0 1 3

So it wouldn't matter if in a step a decimal value popped up.

You have to take decimal value into consideration because then the program would crash.
Last edited on
Never mind I solved it finally. Phew, this one was hard.

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

using namespace std;

double matrix[1000][1000];

typedef class{
public:
    int row;
    int column;
void createPos(int a, int b){
row = a;
column = b;
}
}Position;

string findType (Position current){
string type;
if (current.row==current.column){
    type = "main";
}
else if (current.row!=current.column){
    type = "else";
}
return type;
}

bool hasFinished (Position current){
bool finished = false;
if (current.row==current.column && matrix[current.row][current.column]==1){
    finished = true;
}
else if (current.row!=current.column && matrix[current.row][current.column]==0){
    finished = true;
}
return finished;
}

Position findPos (Position current, int n){
Position compare;
int row = 0;
int column = current.column;
for (int i=0; i<n; i++){
    if (matrix[i][column]!=0 && i!=current.row){
        row = i;
    }
}
compare.createPos(row, column);
return compare;
}

double findTimes (Position current, Position compare, string type){
double times;
if (type=="main"){
    times = 1 / matrix[current.row][current.column];
}
else if (type=="else"){
    times = matrix[current.row][current.column] / matrix[compare.row][compare.column];
}
return times;
}

void updateRow (Position current, Position compare, double times, string type, int n){
if (type=="main"){
for (int column = 0; column<=n; column++){
    matrix[current.row][column] *= times;
}
}
else if (type=="else"){
    for (int column = 0; column<=n; column++){
        matrix[current.row][column] -= times * matrix[compare.row][column];
    }
}
}

int main(){
int n;
cin>>n;
for (int row = 0; row < n; row ++){
    for (int column = 0; column < n; column ++){
        cin >> matrix[row][column];
    }
    cin>>matrix[row][n];
}
for (int column = 0; column<n; column++){
    for (int row = 0; row<n; row++){
        Position current, compare;
        string type;
        current.createPos(row, column);
        if (hasFinished(current)==false){
            compare = findPos(current, n);
            type = findType (current);
            updateRow(current, compare, findTimes(current, compare, type), type, n);
        }
    }
}
for (int row = 0; row<n; row++){
    for (int column = 0; column<n; column++){
        cout<<matrix[row][column]<<" ";
    }
    cout<<"\n";
}
cout<<"final answer: \n";
for (int row = 0; row<n; row++){
    cout << matrix[row][n]<<"\n";
}
}
Topic archived. No new replies allowed.