Advertisement
Dambosin

prostieiteracii

Mar 25th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1.  
  2. #include "pch.h"
  3. #include <iostream>
  4.  
  5.  
  6. using namespace std;
  7. int main()
  8. {  
  9.     setlocale(LC_ALL, "Russian");
  10.     double ebs;
  11.     int n;
  12.     double arr[100][100];  
  13.     cout << "Введите размер матрицы" << endl;
  14.     cin >> n;
  15.     cout << "Введите матрицу" << endl;
  16.     for (int i = 0; i < n; i++) {
  17.         for (int j = 0; j <= n; j++) {
  18.             cin >> arr[i][j];
  19.         }
  20.     }
  21.     cout << "Введите максимальную погрешность " << endl;
  22.     cin >> ebs;
  23.     for (int i = 0; i < n; i++) {
  24.  
  25.         int pos = 0;
  26.         for (int j = 0; j < n; j++) {
  27.             if (arr[pos][i] < arr[j][i]) {
  28.                 pos = j;
  29.             }
  30.         }
  31.         double z[100];
  32.         for (int j = 0; j <= n; j++) {
  33.             z[j] = arr[i][j];
  34.             arr[i][j] = arr[pos][j];
  35.             arr[pos][j] = z[j];
  36.         }
  37.     }
  38.     cout << endl;
  39.     for (int i = 0; i < n; i++) {
  40.         for (int j = 0; j <= n; j++) {
  41.             cout << arr[i][j] << " ";
  42.         }
  43.         cout << endl;
  44.     }
  45.  
  46.     double x[100];
  47.     double xxx[100];
  48.     bool z;
  49.     int k = 0;
  50.     for (int i = 0; i < n; i++) xxx[i] = 0;
  51.     do{
  52.         k++;
  53.         z = false;
  54.         for (int i = 0; i < n; i++) {
  55.             x[i] = arr[i][n];
  56.  
  57.             for (int j = 0; j < n; j++) {
  58.                 if (j != i) {
  59.                     x[i] -= arr[i][j] * xxx[j];
  60.                 }
  61.             }
  62.             x[i] /= arr[i][i];
  63.             if (fabs(xxx[i] - x[i]) > ebs) {
  64.                 z = true;
  65.             }
  66.         }
  67.         for (int j = 0; j < n; j++) {
  68.             xxx[j] = x[j];
  69.         }
  70.     } while (z);
  71.     for (int j = 0; j < n; j++) {
  72.         cout <<"x"<<j+1<<" = "<< xxx[j] << endl;
  73.     }
  74.     cout << "Количество итераций " << k;
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement