system solver. need check

hello i have written a program that solves linear system using rref on matrixs.
i believe everything is working correctly but could someone check if there are no errors or things i have forgotten?

thanks in advance
jannes braet


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
#include "stdafx.h"
#include <iostream>
using namespace std;

const int rij=3,kolom=4;

void output(double disp[][kolom]) {
	for (int i=0;i<rij;i++) {
		for (int j=0;j<kolom;j++) {
				cout<<disp[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}

void swaprow(double matr[][kolom],int k) {
	int buffer[kolom];

	for (int i=0;i<kolom;i++) {
		buffer[i]=matr[k][i];
	}

	for (int i=k+1;i<rij;i++) {
		for (int j=0;j<kolom;j++) {
			matr[i-1][j]=matr[i][j];
		}
	}

	for (int j=0;j<kolom;j++) {
		matr[rij-1][j]=buffer[j];
	}

	//output(matr);
}

void solve()
{
	
	double matrix[rij][kolom]={{0,2,3,4},{0,8,12,7},{1,16,4,0}};
	
	for (int k=0;k<rij;k++) {

			for (int j=kolom-1;j>=0;j--) {
				int count=0;
				again:
					if (matrix[k][k]!=0) {
						matrix[k][j]=matrix[k][j]/matrix[k][k];
					} else {
						swaprow(matrix,k);
						count++;
						if (count<kolom) {
							goto again;
						} else {
							cout<<"no solutions"<<endl;
							system("pause");
						}
					}
			}

			output(matrix);

			for (int i=rij-1;i>=0;i--) {
				if (i==k) {continue;}
				for (int j=kolom-1;j>=0;j--) {
					matrix[i][j]=matrix[i][j]-(matrix[i][k]*matrix[k][j]);
				}
			}

		output(matrix);	
		
	}
	
}

int main()
{
	solve();
	system("pause");
	return 0;
}


Last edited on
bump. anyone?
1. Read up on code style/practices so that it is easier read.
2. Avoid system() calls. There are other ways of pausing the console so it doesnt close immediately, such as...


1
2
     cin.ignore();
    cin.get();


3. Dont use goto. There are others who can better explain its flaws, I will just leave this link here.

http://newdata.box.sk/bx/c/htm/ch07.htm#Heading5

Instead of goto, you should probably use break or continue, and restructure your loops if necessary.

4. Give global constants a way of identifying them. For example, the more common practice is to use all upper case letters, like...

const int MY_GLOBAL_INTEGER;


Topic archived. No new replies allowed.