Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- const int n = 4;
- vector<vector<double>> a(n, vector<double>(n, 0));
- vector<double> b(n, 0), x(n, 0);
- void reverse(int k, int r)
- {
- double c;
- for (int j = k; j < n; j++)
- {
- c = a[k][j];
- a[k][j] = a[r][j];
- a[r][j] = c;
- }
- c = b[k];
- b[k] = b[r];
- b[r] = c;
- }
- void dv(int k)
- {
- for (int j = k + 1; j < n; j++)
- a[k][j] /= a[k][k];
- b[k] /= a[k][k];
- }
- void clear(int k, int r)
- {
- for (int j = k + 1; j < n; j++)
- a[r][j] -= a[k][j] * a[r][k];
- b[r] -= b[k] * a[r][k];
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- cout << "Введите коэфиценты" << endl;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- cin >> a[i][j];
- cout << "Введите свободные члены" << endl;
- for (int i = 0; i < n; i++)
- cin >> b[i];
- for (int k = 0; k < n - 1; k++)
- {
- if (a[k][k] == 0)
- {
- int i = k + 1;
- while (a[i][k] == 0)
- i++;
- reverse(k, i);
- }
- dv(k);
- for (int i = k; i < n; i++)
- clear(k, i);
- }
- dv(n - 1);
- for (int i = n-1; i >= 0; i--)
- {
- x[i] = b[i];
- for (int j = i + 1; j < n; j++)
- x[i] -= a[i][j] * x[j];
- cout << "x[" << i << "]=" << x[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment