Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <iomanip>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8.     //const int n=5;
  9.  
  10.     int main()
  11.     {
  12.         int n;
  13.         //double A,B,C,D;
  14.         cout << " Metoda Thomasa\n";
  15.         cout << " Wprowadz stopien macierzy: ";
  16.         cin >> n;
  17.     double A[n],B[n],C[n],D[n];
  18.         /*A = new double [n];
  19.         B = new double [n];
  20.         C = new double [n];
  21.         D = new double [n];*/
  22.  
  23.     srand(time(0));
  24.     for(int j = 0; j < n; j ++)
  25.     {
  26.         A[j]=(rand()%20) + 1;
  27.         B[j]=(rand()%20) + 1;
  28.         C[j]=(rand()%20) + 1;
  29.         D[j]=(rand()%20) + 1;
  30.     }
  31.     cout << "\nWypelniona macierz A: ";
  32.  
  33.     for(int j = 0; j < n; j ++)
  34.         {
  35.             cout << A[j] << " ";
  36.         }
  37.         cout << endl;
  38.  
  39.     cout << "\nWypelniona macierz B: ";
  40.  
  41.     for(int j = 0; j < n; j ++)
  42.         {
  43.             cout << B[j] << " ";
  44.         }
  45.         cout << endl;
  46.  
  47.     cout << "\nWypelniona macierz C: ";
  48.  
  49.     for(int j = 0; j < n; j ++)
  50.         {
  51.             cout << C[j] << " ";
  52.         }
  53.         cout << endl;
  54.  
  55.     cout << "\nWypelniona macierz D: ";
  56.  
  57.     for(int j = 0; j < n; j ++)
  58.         {
  59.             cout << D[j] << " ";
  60.         }
  61.         cout << endl;
  62.  
  63. /*
  64.         for(int i = 0; i < n; i++)
  65.         {
  66.             A[i] = new double[n];
  67.             B[i] = new double[n];
  68.             C[i] = new double[n];
  69.             D[i] = new double[n];
  70.         }
  71. */
  72.  
  73.         /*double A[n]={0,2,1,1,1};
  74.         double B[n]={1,2,2,2,1};
  75.         double C[n]={3,2,1,6,0};
  76.         double D[n]={1,5,5,2,2};*/
  77.  
  78.         double X[n];
  79.         double beta[n];
  80.         double gamma[n];
  81.  
  82.         beta[0]=-(C[0]/B[0]);
  83.         gamma[0]=(D[0]/B[0]);
  84.  
  85.         for(int i=0;i<n;i++)
  86.         {
  87.             beta[i]=-(C[i]/(A[i]*beta[i-1]+B[i]));
  88.             gamma[i]=(D[i]-A[i]*gamma[i-1])/(A[i]*beta[i-1]+B[i]);
  89.         }
  90.  
  91.         for(int i=0;i<n;i++)
  92.         {
  93.             cout<<"\nWspolczynnik Beta " <<i+1<< " = "<< beta[i]<<endl;
  94.             cout<<"Wspolczynnik Gamma " <<i+1<< " = " << gamma[i]<<endl;
  95.  
  96.         }
  97.         X[n]=gamma[n];
  98.  
  99.         for(int i=(n-1);i>=0;i--)
  100.         {
  101.             X[i]=(beta[i]*X[i+1]+gamma[i]);
  102.         }
  103.  
  104.         for(int i=0;i<n;i++)
  105.         {
  106.         cout<<"\n X" << i+1  << " = "<< X[i]<<"\n";
  107.    }
  108.  
  109.         return 0;
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement