Facebook
From Ungracious Wigeon, 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 225
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.    int i,n;
  8.    double a[n];
  9.    double b[n];
  10.    double c[n];
  11.    double d[n];
  12.    double x[n];
  13.    double bet[n];
  14.    double gam[n];
  15.  
  16.    cout << "Podaj n: ";
  17.    cin >> n;
  18.  
  19.    for(int i=0; i<n; i++)
  20.    {
  21.        cout << "Podaj A[: "<<i<<"] = ";
  22.        cin >> a[i];
  23.        cout << "Podaj B[: "<<i<<"] = ";
  24.        cin >> b[i];
  25.        cout << "Podaj C[: "<<i<<"] = ";
  26.        cin >> c[i];
  27.        cout << "Podaj D[: "<<i<<"] = ";
  28.        cin >> d[i];
  29.    }
  30.  
  31.    a[0]=c[n-1]=0;
  32.  
  33.    bet[0]=-c[0]/b[0];
  34.    gam[0]=d[0]/b[0];
  35.  
  36.    for(int i=1; i<n; i++)
  37.    {
  38.        bet[i]=-c[i]/((a[i]*bet[i-1])+b[i]);
  39.        gam[i]=(d[i]-a[i]*gam[i-1])/((a[i]*bet[i-1])+b[i]);
  40.     }
  41.  
  42.    x[n-1]=gam[n-1];
  43.  
  44.    for(int i=n-2; i>=0; i--)
  45.    {
  46.        x[i]=bet[i]*x[i+1]+gam[i];
  47.    }
  48.  
  49.    for(int i=0; i<n; i++)
  50.    {
  51.        cout<<x[i] << endl;
  52.    }
  53.    return 0;
  54. }