Advertisement
Czopski

Metoda el. gaussa z wyświetleniem danych

Jun 4th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. int n,i,j,k;
  8. cout.precision(4);
  9. cout.setf(ios::fixed);
  10. cout<<"podaj liczbe niewiadomych: "<<endl;
  11. cin>>n;
  12. float a[n][n+1], x[n];
  13. cout<<"wpisz niewiadome: "<<endl;
  14. for(i=0;i<n;i++)
  15.     for(j=0;j<=n;j++)
  16.         cin>>a[i][j];
  17.  
  18. for(i=0;i<n;i++)
  19.     for(k=i+1;k<n;k++)
  20.         if(a[i][j]<a[k][i])
  21.             for (j=0;j<=n;j++){
  22.                 double temp = a[i][j];
  23.                 a[i][j] = a[k][j];
  24.                 a[k][j]=temp;
  25.             }
  26. cout<<"Macierz po wykonaniu zmiany rzedow:"<<endl;
  27. for(i=0;i<n;i++){
  28.     for(j=0;j<=n;j++)
  29.         cout<<a[i][j]<<setw(16);
  30.         cout<<"\n";
  31. }
  32. for(i=0;i<n;i++)
  33.     for(k=i+1;k<n;k++){
  34.     double t = a[k][i]/a[i][i];
  35.     for(j=0;j<=n;j++){
  36.             a[k][j]=a[k][j]-t*a[i][j];
  37.     }
  38. }
  39. cout<<"macierz po eliminacji gaussa: "<<endl;
  40. for(i=0;i<n;i++){
  41.     for(j=0;j<=n;j++)
  42.         cout<<a[i][j]<<setw(16);
  43.     cout<<"\n";
  44. }
  45.     for (i=n-1;i>=0;i--)                //back-substitution
  46.     {                        //x is an array whose values correspond to the values of x,y,z..
  47.         x[i]=a[i][n];                //make the variable to be calculated equal to the rhs of the last equation
  48.         for (j=i+1;j<n;j++)
  49.             if (j!=i)            //then subtract all the lhs values except the coefficient of the variable whose value                                   is being calculated
  50.                 x[i]=x[i]-a[i][j]*x[j];
  51.         x[i]=x[i]/a[i][i];            //now finally divide the rhs by the coefficient of the variable to be calculated
  52.     }
  53.     cout<<"\nThe values of the variables are as follows:\n";
  54.     for (i=0;i<n;i++)
  55.         cout<<x[i]<<endl;            // Print the values of x, y,z,....
  56.     return 0;
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement