Skip to content

Instantly share code, notes, and snippets.

@dz0
Last active January 20, 2017 08:50
Show Gist options
  • Select an option

  • Save dz0/7afdf8a4b900ae4ecab6df74b3eeafd4 to your computer and use it in GitHub Desktop.

Select an option

Save dz0/7afdf8a4b900ae4ecab6df74b3eeafd4 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// https://en.wikipedia.org/wiki/Gaussian_elimination#Example_of_the_algorithm
double M[3][4] = {
{ 2, 1, -1, 8}, // 0
{-3, -1, 2, -11}, // 1
{-2, 1, 2, -3} // 2
};
int n=3; // kiek lygčių
/*
// Testavimui - 4 lygtys: http://www.1728.org/unknwn4.htm
int n=4;
double M[4][5] = {
{ 2, 3, 4, -5, -6}, // 0
{ 6, 7, -8, 9, 96}, // 1
{10, 11, 12, 13 , 312}, // 2
{14, 15, 16, 17, 416}, // 3
};
*/
void printM(){
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
cout << M[row][col] << " ";
}
cout << "| " << M[row][n] << endl;
}
}
void add_row(int src, int target, double k){
// src - nr eilutės, iš kurios pridedame
// k - koeficientas, iš kurio padauginam
// target - nr eilutės, į kurią pridedame
for (int i = 0; i < n+1 ; i++) {
// k*M[src][i] + M[target][i]
}
}
int main() {
cout << "Pradzioj" << endl;
printM();
cout << "Testuojam pridet 0 prie 2 eilutes" << endl;
add_row(0, 2, 1); // koeficientas 1
printM();
cout << "\n nunulinam po istrizaine \n";
// iš eilės einam per stulpelius
for (int col = 0; col < n-1; col++) // col - nurodo, kurį stulpelį taikom nunulint
{
int src_row = col; // col atitinka src parametrą, nes leidžiamės įstrižaine (joje stulpelis sutampa su eilute) -- aiškumui pasidarom tarpinį kintamąjį..
//for (int row = ..) // kiekvienai žemesnei eilutei
{
//double k = ... ; // paskaičiuojam koef
// add_row( ); // pridedam src eilutę kur reikia ;)
}
}
/*
kaip cikluose keičiasi nr.:
kai n=3: col/src row
0 1, 2
1 2
kai n=4: 0 1, 2, 3
1 2, 3
2 3
*/
printM();
cout << "\n nunulinam virs istrizaines \n";
for (int col = n-1; col > 0; col--)
{
//for (int row = )
{
//double k = ...
//add_row( );
}
}
cout << "\n is istrizaines surenkam ats \n";
for (int i = 0; i < n; i++) {
// cout << i << " " << ??? << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment