Skip to content

Instantly share code, notes, and snippets.

@cymruu
Created October 30, 2017 18:54
Show Gist options
  • Save cymruu/f011557a8e9db927b5fcbed3be9de7b2 to your computer and use it in GitHub Desktop.
Save cymruu/f011557a8e9db927b5fcbed3be9de7b2 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <conio.h>
#include <ctime>
using namespace std;
void printMatrix(double *tab, size_t rows, string name) {
cout << "----------------" << endl;
for (size_t i = 0; i < rows; i++)
{
std::cout << name << i << "=" << tab[i] << endl;
}
cout << "----------------" << endl;
}
void printDiagonal(double **tab, size_t rows) {
for (size_t i = 0; i < rows; i++)
{
for (size_t e = 0; e < 3; e++) {
std::cout << "" << tab[i][e] << "\t";
}
std::cout << endl;
}
}
int main()
{
srand(time(nullptr));
double **A, *W, *X;
int i,n;
cout << "Podaj liczbe rownan" << endl;
cin >> n;
A = new double *[n];
W = new double[n];
X = new double[n];
double *beta = new double[n];
double *gamma = new double[n];
for (i = 0; i < n; i++) {
A[i] = new double[3];
}
for (size_t i = 0; i < n; i++)
{
W[i] = 3;
}
if (n == 5) {
A[0][0] = 0;
A[0][1] = 2;
A[0][2] = 2;
A[1][0] = 3;
A[1][1] = 1;
A[1][2] = 1;
A[2][0] = 1;
A[2][1] = 2;
A[2][2] = 4;
A[3][0] = 1;
A[3][1] = 1;
A[3][2] = 1;
A[4][0] = 2;
A[4][1] = 2;
A[4][2] = 0;
W[0] = 2;
W[1] = 6;
W[2] = 4;
W[3] = 1;
W[4] = 4;
}
else {
double insert;
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < 3; j++)
{
cout << "podaj element o wspolrzednych: " << i << "," << j << endl;
cin >> insert;
A[i][j] = insert;
}
}
for (size_t i = 0; i < n; i++)
{
cout << "podaj wynik o wspolrzednych: " << i << endl;
cin >> insert;
W[i] = insert;
}
}
printDiagonal(A, n);
printMatrix(W, n, "wyniki");
//ustawiamy pierwszy i ostatni element przekatnej na 0
A[0][0] = 0; //a1 = 0
A[n-1][2] = 0; //cn = 0
//obliczamy startowe beta i gamma
beta[0] = -(A[0][2]/A[0][1]);
gamma[0] = W[0]/A[0][1];
//obliczamy beta i gamma od "przodu"
for (size_t i = 1; i < n; i++)
{
beta[i] = -(A[i][2]/(A[i][0]*beta[i-1]+A[i][1]));
gamma[i] = (W[i] - A[i][0] * gamma[i - 1]) / ((A[i][0] * beta[i - 1]) + A[i][1]);
}
printMatrix(beta, n, "beta");
printMatrix(gamma, n, "gamma");
//obliczamy x od "tylu"
X[n] = gamma[n];
for (int i = n; i>=0; i--)
{
X[i] = beta[i] * X[i+1] + gamma[i];
// cout << X[i];
}
printMatrix(X, n, "X");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment