Advertisement
Norvager

XV

Oct 27th, 2021
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. int main() {
  8. freopen("input.txt", "r", stdin);    
  9.     int i, j, n, m;
  10.     //создаем массив
  11.     cout << "Number of equations: ";
  12.     cin >> n;
  13.     cout << "Number of variables: ";
  14.     cin >> m;
  15.     m += 1;
  16.     double matrix[3][4];
  17.  
  18.     double e;
  19.     cout << "E: ";
  20.     cin >> e;
  21.     //инициализируем
  22.     for (i = 0; i < n; i++) {
  23.         for (j = 0; j < m; j++) {
  24.             cout << " Element " << "[" << i + 1 << ", " << j + 1 << "]: ";
  25.             cin >> matrix[i][j];
  26.         }
  27.     }
  28.     //выводим массив
  29.     cout << "matrix: " << endl;
  30.     for (i = 0; i < n; i++)
  31.     {
  32.         for (j = 0; j < m; j++)
  33.             cout << matrix[i][j] << " ";
  34.         cout << endl;
  35.     }
  36.     cout << endl;
  37.     //Метод простых итераций
  38.     int iteratoin = 0;
  39.     double tmpOpenX, openX = NULL;
  40.     double answer[3] = {0, 0, 0};
  41.     double tmpAnswer[3] = {0, 0, 0,};
  42.     for (i = 0; i < n; i++) {
  43.         double tmp = matrix[i][i];
  44.         for (j = 0; j < m; j++) {
  45.             matrix[i][j] /= (3!=j)? -tmp : tmp;
  46.             cout << matrix[i][j] << " ";
  47.         }
  48.         cout << endl;
  49.     }
  50.     for (i = 0; i < n; i++) {
  51.         answer[i] = matrix[i][3];
  52.     }
  53. m1:
  54.     for (i = 0; i < n; i++) {
  55.         for (j = 0; j < m; j++) {
  56.             if (j == i) {
  57.                 continue;
  58.             }
  59.             tmpAnswer[i] += matrix[i][j] * (j!=3)?answer[j]:1;
  60.         }
  61.         cout << tmpAnswer[i] << " ";
  62.     }
  63.     cout << endl;
  64.     memcpy(answer, tmpAnswer, sizeof(double)*n + 1);
  65.     tmpAnswer[0] = tmpAnswer[1] = tmpAnswer[2] = 0;
  66.     tmpOpenX = max(fabs(answer[0]),max(fabs(answer[1]), fabs(answer[2])));
  67.     if (!openX) {
  68.         openX = tmpOpenX;
  69.     } else if(e > tmpOpenX - openX){
  70.         goto m2;
  71.     }
  72.     goto m1;
  73.  
  74. m2:
  75.     //Выводим решения
  76.     for (i = 0; i < n; i++)
  77.         cout << answer[i] << " ";
  78.     cout << endl;
  79.    
  80.     system("pause");
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement