gashink_t

method_progonki(lab_3, vm)

Feb 24th, 2021 (edited)
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void print(float** MTRX_ORIG, int N);
  7. float** create(int N);
  8. float* alg_progon(float** MTRX, int N);
  9.  
  10. int main()
  11. {
  12.     int n;
  13.     float** mtrx;
  14.     cout << "Enter the number of equations n = ";
  15.     cin >> n;
  16.     mtrx = create(n);
  17.     cout << endl << "Your matrix:" << endl;
  18.     print(mtrx, n);
  19.    
  20.     float* answer = new float[n];
  21.     answer = alg_progon(mtrx, n);
  22.     cout << "Answer:\n";
  23.     for (int i = 0; i < n; i++)
  24.     {
  25.         cout << "x" << i + 1 << " = " << round(answer[i] * 100) / 100 << endl;
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. void print(float** MTRX_ORIG, int N)
  32. {
  33.     for (int i = 0; i < N; i++)
  34.     {
  35.         for (int j = 0; j <= N; j++)
  36.         {
  37.             if (j < N)
  38.                 cout << setw(10) << round(MTRX_ORIG[i][j] * 1000) / 1000;
  39.             else
  40.                 cout << setw(10) << "|" << round(MTRX_ORIG[i][j] * 1000) / 1000;
  41.         }
  42.         cout << endl;
  43.     }
  44.     cout << endl;
  45. }
  46.  
  47. float** create(int N)
  48. {
  49.     float** MTRX_ORIG = new float* [N];
  50.     for (int i = 0; i < N; i++)
  51.     {
  52.         MTRX_ORIG[i] = new float[N + 1];
  53.     }
  54.  
  55.     cout << endl << "Enter the element matrix:" << endl;
  56.     for (int i = 0; i < N; i++)
  57.         for (int j = 0; j <= N; j++)
  58.         {
  59.             cin >> MTRX_ORIG[i][j];
  60.         }
  61.     return MTRX_ORIG;
  62. }
  63.  
  64. float* alg_progon(float** MTRX, int N)
  65. {
  66.     float* answer = new float[N];
  67.     float* bet = new float[N];
  68.     float* alf = new float[N];
  69.     float y;
  70.  
  71.     for (int i = 0; i < N; i++)
  72.     {
  73.         if (i == 0)
  74.         {
  75.             y = MTRX[i][i];
  76.             alf[i] = -(MTRX[i][i + 1]) / y;
  77.             bet[i] = MTRX[i][N] / y;
  78.         }
  79.         else
  80.         {
  81.             y = MTRX[i][i] + MTRX[i][i - 1] * alf[i - 1];
  82.             alf[i] = (-MTRX[i][i + 1]) / y;
  83.             bet[i] = (MTRX[i][N] - MTRX[i][i - 1] * bet[i - 1]) / y;
  84.         }
  85.  
  86.         cout << "y" << i + 1 << " = " << round(y*100)/100 << endl;
  87.         cout << "alf" << i + 1 << " = " << round(alf[i] * 100) / 100 << endl;
  88.         cout << "bet" << i + 1 << " = " << round(bet[i] * 100) / 100 << endl;
  89.         cout << endl;
  90.     }
  91.  
  92.     for (int i = N - 1; i >= 0; i--)
  93.     {
  94.         if (i == N - 1)
  95.             answer[i] = bet[i];
  96.         else answer[i] = alf[i] * answer[i + 1] + bet[i];
  97.     }
  98.  
  99.     return answer;
  100. }
  101.  
  102.  
Add Comment
Please, Sign In to add comment