Advertisement
gashink_t

method Gauss(lab_1, BM)

Feb 3rd, 2021 (edited)
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void print(float** MTRX, int N);
  7. float** create(int N);
  8. float** create_rand(int N);
  9. void m_Gauss(float** MTRX, int N);
  10. int m_modif_Gauss(float** MTRX, int N);
  11. void answer(float* T_MTRX);
  12. void answer_2(float* T_MTRX, int N);
  13. int rank_mtrx(int N, float** MTRX);
  14.  
  15. int main()
  16. {
  17.     int n, c;
  18.     float** mtrx;
  19.     cout << "Enter the number of equations n = ";
  20.     cin >> n;
  21.     cout << "Would you like to create the matrix? Random(0)/By hand(1)" << endl << "->";
  22.     cin >> c;
  23.     if (c == 0)
  24.         mtrx = create_rand(n);
  25.     else
  26.         mtrx = create(n);
  27.  
  28.     cout << endl << "Your matrix:" << endl;
  29.     print(mtrx, n);
  30.  
  31.     int rank = rank_mtrx(n,mtrx);
  32.     cout << endl << "Rank = " << rank << endl;
  33.     if (rank < n)
  34.     {
  35.         cout << "The system has infinitely many solutions!" << endl;
  36.         return 0;
  37.     }
  38.  
  39.     cout << "What method would you like to solve the matrix by? Simple(0)/Modified(1)" << endl << "->";
  40.     cin >> c;
  41.     if (c == 0)
  42.         m_Gauss(mtrx, n);
  43.     else
  44.         m_modif_Gauss(mtrx, n);
  45.    
  46.     return 0;
  47. }
  48.  
  49. void print(float** MTRX, int N)
  50. {
  51.     for (int i = 0; i < N; i++)
  52.     {
  53.         for (int j = 0; j < N + 1; j++)
  54.         {
  55.             if (j < N)
  56.                 cout << setw(10) << round(MTRX[i][j]*100)/100;
  57.             else
  58.                 cout << setw(10) << "|" << round(MTRX[i][j] * 100) / 100;
  59.         }
  60.         cout << endl;
  61.     }
  62. }
  63.  
  64. float** create(int N)
  65. {
  66.     float** MTRX = new float* [N];
  67.     for (int i = 0; i < N; i++)
  68.     {
  69.         MTRX[i] = new float[N + 1];
  70.     }
  71.    
  72.     cout << endl << "Enter the element matrix:" << endl;
  73.     for (int i = 0; i < N; i++)
  74.         for (int j = 0; j < N + 1; j++)
  75.         {
  76.             cin >> MTRX[i][j];
  77.         }
  78.     return MTRX;
  79. }
  80.  
  81. float** create_rand(int N)
  82. {
  83.     float** MTRX = new float* [N];
  84.     for (int i = 0; i < N; i++)
  85.     {
  86.         MTRX[i] = new float[N + 1];
  87.     }
  88.     for (int i = 0; i < N; i++)
  89.         for (int j = 0; j < N + 1; j++)
  90.         {
  91.             MTRX[i][j] = rand() % 10;
  92.         }
  93.     return MTRX;
  94. }
  95.  
  96. void m_Gauss(float** MTRX, int N)
  97. {
  98.     for (int i = 0; i < N; i++)
  99.     {
  100.         float t = MTRX[i][i];
  101.         for (int j = i; j <= N; j++)
  102.             MTRX[i][j] = MTRX[i][j] / t;
  103.         for (int j = i + 1; j < N; j++)
  104.         {
  105.             t = MTRX[j][i];
  106.             for (int k = i; k <= N; k++)
  107.                 MTRX[j][k] = MTRX[j][k] - t * MTRX[i][k];
  108.         }
  109.         cout << i+1 << ":" << endl;
  110.         print(MTRX, N);
  111.     }
  112.  
  113.     cout << endl << "Your matrix after the decision by the Gauss method:" << endl;
  114.     print(MTRX, N);
  115.  
  116.     /*Находим неизвестные переменные*/
  117.     float* t_MATRX = new float[N];
  118.     t_MATRX[N - 1] = MTRX[N - 1][N];
  119.     for (int i = N - 2; i >= 0; i--)
  120.     {
  121.         t_MATRX[i] = MTRX[i][N];
  122.         for (int j = i + 1; j < N; j++)
  123.         {
  124.             t_MATRX[i] = t_MATRX[i] - MTRX[i][j] * t_MATRX[j];
  125.         }
  126.     }
  127.  
  128.     answer(t_MATRX);
  129. }
  130.  
  131. int m_modif_Gauss(float** MTRX, int N)
  132. {
  133.     float t, max = 0;
  134.     int i_max = 0;
  135.     for (int i = 0; i < N; i++)
  136.     {
  137.         if (abs(MTRX[i][0]) > max)
  138.         {
  139.             max = abs(MTRX[i][0]);
  140.             i_max = i;
  141.         }
  142.     }
  143.     if (i_max > 0)
  144.     {
  145.         for (int i = 0; i <= N; i++)
  146.         {
  147.             t = MTRX[0][i];
  148.             MTRX[0][i] = MTRX[i_max][i];
  149.             MTRX[i_max][i] = t;
  150.         }
  151.     }
  152.     cout << endl;  print(MTRX, N);
  153.  
  154.     for (int i = N; i >= 0; i--)
  155.     {
  156.         MTRX[0][i] /= MTRX[0][0];
  157.     }
  158.     for (int i = 1; i < N; i++)
  159.     {
  160.         for (int j = N; j >= 0; j--)
  161.         {
  162.             MTRX[i][j] -= MTRX[i][0] * MTRX[0][j];
  163.         }
  164.     }
  165.     cout << endl;  print(MTRX, N);
  166.    
  167.     for (int i = N; i > 0; i--)
  168.     {
  169.         MTRX[1][i] /= MTRX[1][1];
  170.     }
  171.     for (int i = 0; i < N; i++)
  172.     {
  173.         for (int j = N; j > 0; j--)
  174.         {
  175.             if (i != 1)
  176.             {
  177.                 MTRX[i][j] -= MTRX[i][1] * MTRX[1][j];
  178.             }
  179.         }
  180.     }
  181.  
  182.     for (int i = N; i > 1; i--)
  183.     {
  184.         MTRX[2][i] /= MTRX[2][2];
  185.     }
  186.     for (int i = 0; i < N - 1; i++)
  187.     {
  188.         for (int j = N; j > 1; j--)
  189.         {
  190.             MTRX[i][j] -= MTRX[i][2] * MTRX[2][j];
  191.         }
  192.     }
  193.  
  194.     cout << endl << "Your matrix after the decision by the modif. Gauss method:" << endl;
  195.     print(MTRX, N);
  196.  
  197.     float* t_MATRX = new float[N];
  198.     for (int i = 0; i < N; i++)
  199.         t_MATRX[i] = MTRX[i][N];
  200.  
  201.     answer(t_MATRX);
  202.     return 0;
  203. }
  204.  
  205. void answer(float* T_MTRX)
  206. {
  207.     cout << "Answer: " << endl;
  208.     cout << "x = " << round(T_MTRX[0]*100)/100 << endl;
  209.     cout << "y = " << round(T_MTRX[1] * 100) / 100 << endl;
  210.     cout << "z = " << round(T_MTRX[2] * 100) / 100 << endl;
  211. }
  212.  
  213. void answer_2(float* T_MTRX, int N)
  214. {
  215.     for (int i = 0; i < N; i++)
  216.     {
  217.         cout << "x" << i + 1 << " = " << T_MTRX[i] << endl;
  218.     }
  219. }
  220.  
  221. int rank_mtrx(int N, float** MTRX) {
  222.     float** MTRX_2 = new float* [N];
  223.     for (int i = 0; i < N; i++)
  224.     {
  225.         MTRX_2[i] = new float[N + 1];
  226.     }
  227.     for (int i = 0; i < N; i++)
  228.         for (int j = 0; j < N + 1; j++)
  229.         {
  230.             MTRX_2[i][j] = MTRX[i][j];
  231.         }
  232.     int rank = N;
  233.     int max = MTRX_2[0][0];
  234.     for (int j = 0; j <= N; j++)
  235.     {
  236.         for (int i = 0; i < N; i++)
  237.             if (MTRX_2[i][j] > max)
  238.                 max = MTRX_2[i][j];
  239.         if (abs(max) == 0) rank--;
  240.     }
  241.  
  242.     for (int i = 0; i < N; i++)
  243.     {
  244.         float t = MTRX_2[i][i];
  245.         for (int j = i; j <= N; j++)
  246.             MTRX_2[i][j] = MTRX_2[i][j] / t;
  247.         for (int j = i + 1; j < N; j++)
  248.         {
  249.             t = MTRX_2[j][i];
  250.             for (int k = i; k <= N; k++)
  251.                 MTRX_2[j][k] = MTRX_2[j][k] - t * MTRX_2[i][k];
  252.         }
  253.     }
  254.     int j;
  255.     for (int i = 0; i < N; i++)
  256.     {
  257.         for (j = i + 1; j < N && !MTRX_2[i][j]; j++);
  258.         if (j == N + 1) rank--;
  259.     }
  260.        
  261.  
  262.     return rank;
  263. }
  264.  
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement