Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- void wyswietlMacierz(double l[3][3])
- {
- for (int j = 0 ; j < 3 ; j ++)
- {
- cout << "| ";
- for (int i = 0 ; i < 3 ; i ++) cout << l[j][i] << " ";
- cout << "|" << endl;
- }
- }
- void stworzMacierz(int n, double a[3][3], double l[3][3])
- {
- double suma;
- for (int s = 0 ; s < n ; s ++)
- {
- for (int j = s ; j < n ; j ++)
- {
- suma = 0;
- for (int i = 0 ; i < s ; i ++) suma += l[j][i] * l[s][i];
- l[j][s] = a[j][s] - suma;
- if (s == j) l[j][s] = sqrt (l[j][s]);
- else l[j][s] /= l[s][s];
- }
- }
- wyswietlMacierz(l);
- }
- int main ()
- {
- double a[3][3] = {{1, 2, 3}, {2, 8, 10}, {3, 10, 22}};
- double b[3] = {1, 3, 7};
- double l[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
- double x[3];
- double y[3];
- int n = 3;
- float s = 0;
- double t;
- cout << "Stworzenie macierzy L" << endl;
- stworzMacierz(n, a, l);
- cout << endl << "Obliczanie wektora y" << endl;
- y[0] = l[0][0];
- for (int i = 1 ; i < n ; i ++)
- {
- s = 0;
- for (int j = 0 ; j < i ; j ++) s = s + l[i][j] * y[j];
- y[i] = (b[i] - s) / l[i][i];
- }
- for (int i = 0 ; i < 3 ; i ++) cout << "y[" << i << "] = " << y[i] << endl;
- cout << endl << "Transpozycja macierzy L" << endl;
- for (int j = 0 ; j < n - 1 ; j++)
- {
- for (int i = j + 1 ; i < n ; i ++)
- {
- t = l[j][i];
- l[j][i] = l[i][j];
- l[i][j] = t;
- }
- }
- wyswietlMacierz(l);
- cout << endl << "Obliczanie wektora x" << endl;
- x[2] = y[2] / l[2][2];
- x[1] = (y[1] - (l[1][2] * x[2])) / l[1][1];
- x[0] = (y[0] - ((l[0][1] * x[1]) + l[0][2] * x[2])) / l[0][0];
- for (int i = 0 ; i < 3 ; i ++) cout << "x[" << i << "] = " << x[i] << endl;
- }
Add Comment
Please, Sign In to add comment