Facebook
From Wet Curlew, 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 243
  1. #include <iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int i, j, n, s;
  8.  
  9.     cout<<"Podaj n.. ";
  10.     cin>>n;
  11.  
  12.     double C[n][n+1];
  13.     double X[n];
  14.     double suma;
  15.  
  16.  
  17.     for(int i=0;i<n;i++)
  18.     {
  19.         for(int j=0;j<=n;j++)
  20.         {
  21.             cout<<"C["<<i<<"]["<<j<<"]";
  22.             cin>>C[i][j];
  23.         }
  24.     }
  25.  
  26.     for(int s=0;s<n-1;s++)
  27.     {
  28.         for(int i=s+1;i<n;i++)
  29.         {
  30.             for(int j=s+1;j<n+1;j++)
  31.             {
  32.                 C[i][j]=C[i][j]-((C[i][s]*C[s][j])/C[s][s]);
  33.             }
  34.         }
  35.     }
  36.  
  37.     X[n-1]=C[n-1][n]/C[n-1][n-1];
  38.  
  39.     for(int i=n-2;i>=0;i--)
  40.     {
  41.         suma=0;
  42.  
  43.         for(int s=i+1;s<n;s++)
  44.         {
  45.             suma=suma+(C[i][s]*X[s]);
  46.         }
  47.         X[i]=(C[i][n]-suma)/C[i][i];
  48.     }
  49.  
  50.     for(int i=0;i<n;i++)
  51.     {
  52.         if(abs(X[i])<0.005) X[i]=0;
  53.             cout<<X[i]<<endl;
  54.     }
  55. }