Created
May 9, 2014 05:26
-
-
Save VardanGrigoryan/71fa28f3d74a07c86c99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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