Skip to content

Instantly share code, notes, and snippets.

@VardanGrigoryan
Created May 9, 2014 05:26
Show Gist options
  • Save VardanGrigoryan/71fa28f3d74a07c86c99 to your computer and use it in GitHub Desktop.
Save VardanGrigoryan/71fa28f3d74a07c86c99 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size;
std::cin >> size;
std::vector <std::vector <long double> > matrix;
matrix.resize (size);
for (int i = 0; i < size; i++)
{
matrix[i].resize (size + 1);
for (int j = 0; j < size + 1; j++)
{
std::cin >> matrix[i][j];
}
long double eps;
std::cin >> eps;
std::vector <long double> previousVariableValues (size, 0.0);
while (true)
{
std::vector <long double> currentVariableValues (size);
for (int i = 0; i < size; i++)
{
int len = matrix[i].size();
matrix[i][size];
currentVariableValues[i] = matrix[i][size];
for (int j = 0; j < size; j++)
{
if (j < i)
{
currentVariableValues[i] -= matrix[i][j] * currentVariableValues[j];
}
if (j > i)
{
currentVariableValues[i] -= matrix[i][j] * previousVariableValues[j];
}
currentVariableValues[i] /= matrix[i][i];
}
}
long double error = 0.0;
for (int i = 0; i < size; i++)
{
error += abs (currentVariableValues[i] - previousVariableValues[i]);
}
if (error < eps)
{
break;
}
previousVariableValues = currentVariableValues;
}
for (int i = 0; i < size; i++)
{
//printf("%.8llf", previousVariableValues[i]);
std::cout << previousVariableValues[i] << " " << std::endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment