Advertisement
isotonicq

Untitled

Oct 23rd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. //VS
  2. //Wersja poprawiona
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int k, r, n;
  11.  
  12.     cout << "Podaj liczbe rownan: ";
  13.     cin >> r;
  14.     k = r + 1;
  15.     n = k - 1;
  16.  
  17.  
  18.     double **a = new double *[r];
  19.  
  20.     for (int i = 0; i < r; ++i)
  21.     {
  22.         a[i] = new double[k];
  23.         for (int j = 0; j < k; ++j)
  24.         {
  25.             cout << "A[" << i + 1 << "," << j + 1 << "]= ";
  26.             cin >> a[i][j];
  27.         }
  28.         cout << "\n";
  29.     }
  30.  
  31.  
  32.  
  33.  
  34.     for (k = 0; k < n; k++)
  35.     {
  36.         double max = a[k][k];
  37.         r = k;
  38.  
  39.         for (int i = k; i < n; i++)
  40.         {
  41.             if (abs(a[i][k]) > max)
  42.             {
  43.                 max = abs(a[i][k]);
  44.                 r = i;
  45.             }
  46.         }
  47.  
  48.         if (fabs(max) <1E-20)
  49.         {
  50.             cout << "Macierz ukladu osobliwa" << endl;
  51.             break;
  52.         }
  53.  
  54.         for (int j = k; j < n + 1; j++)
  55.         {
  56.             double tmp = a[k][j];
  57.             a[k][j] = a[r][j];
  58.             a[r][j] = tmp;
  59.         }
  60.         double p = a[k][k];
  61.         for (int j = k; j < n + 1; j++)
  62.         {
  63.             double tmp = a[k][j] / p;
  64.             a[k][j] = tmp;
  65.         }
  66.  
  67.         for (int i = 0; i < n; i++)
  68.         {
  69.             if (i != k)
  70.             {
  71.                
  72.                 double g = a[i][k];
  73.                 for (int j = k; j < n + 1; j++)
  74.                 {
  75.                     a[i][j] -= (g * a[k][j]);
  76.  
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     for (int i = 0; i < n; i++)
  82.     {
  83.         for (int j = 0; j < n + 1; j++)
  84.             cout << a[i][j] << "\t";
  85.         cout << endl;
  86.     }
  87.  
  88.     int x=0;
  89.     cin >> x;
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement