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;
}
|