Advertisement
Tobiasz931

Big Fucking Gauss' Gun v2.0

Mar 20th, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9. void wypisz(double**,int);
  10.  
  11. int main(){
  12.     cout.precision(16);
  13.     int n;
  14.     cout<<"podaj n"<<endl;
  15.     cin>>n;
  16.     cout << endl;
  17.     double **tab;
  18.     double **tab2;
  19.     double *A;
  20.    
  21.     time_t now;
  22.     time(&now);
  23.     srand((unsigned int)now);
  24.  
  25.     tab=new double*[n];
  26.     for (int i=0;i<n;i++){
  27.         tab[i]=new double[n+1];
  28.     }
  29.     A=new double[n];
  30.     for(int i=0; i<n; i++){
  31.         //A[i]=pow(2.0f, rand()%10);
  32.         //A[i]=rand()%2000-1000;
  33.         A[i]=rand()/32.931;
  34.         cout << A[i] << endl;
  35.     }
  36.     for (int i=0;i<n;i++){
  37.         double temp=0;
  38.         for (int j=0;j<n;j++){
  39.             //tab[i][j]=pow(2.0f, rand()%10);
  40.             //tab[i][j]=rand()%20-10;
  41.             tab[i][j]=rand()/49.001;
  42.             temp+=tab[i][j]*A[j];
  43.         }
  44.         tab[i][n]=temp;
  45.     }
  46.     tab2=new double*[n];
  47.     for (int i=0;i<n;i++){
  48.         tab2[i]=new double[n+1];
  49.     }
  50.     for(int y=0; y<n; y++)
  51.         for(int x=0; x<n+1; x++)
  52.             tab2[y][x]=tab[y][x];
  53.     //rozwiązanie bez przestawiania
  54.     for(int y=0; y<n; y++){
  55.         double mnoznik=1/tab[y][y];
  56.         for (int x=y;x<n+1;x++){
  57.             tab[y][x]*=mnoznik;
  58.         }
  59.         for(int w=y+1; w<n; w++){
  60.             double mnoznik=tab[w][y]/tab[y][y];
  61.             for(int x=0; x<n+1; x++){
  62.                 tab[w][x]-=tab[y][x]*mnoznik;
  63.             }
  64.         }
  65.     }
  66.     for(int y=n-1; y>=0; y--){
  67.         for(int w=y-1; w>=0; w--){
  68.             tab[w][n]-=tab[y][n]*tab[w][y];
  69.             tab[w][y]=0;
  70.         }
  71.     }
  72.     //cout << endl;
  73.     //for(int i=0; i<n; i++)
  74.         //cout << tab[i][n] << ' ' << abs(tab[i][n]-A[i])+1 << ' ' << abs(tab[i][n]-A[i])/A[i]+1 << endl;
  75.  
  76.     //rozwiązanie z przestawianiem
  77.     for(int y=0; y<n; y++){
  78.         double record=tab2[y][y];
  79.         int ynum=y;
  80.         for(int i=y; i<n; i++){
  81.             if(tab2[i][y]>record){
  82.                 record=tab2[i][y];
  83.                 ynum=i;
  84.             }
  85.         }
  86.         swap(tab2[y], tab2[ynum]);
  87.  
  88.         double mnoznik=1/tab2[y][y];
  89.         for (int x=y;x<n+1;x++){
  90.             tab2[y][x]*=mnoznik;
  91.         }
  92.         for(int w=y+1; w<n; w++){
  93.             double mnoznik=tab2[w][y]/tab2[y][y];
  94.             for(int x=0; x<n+1; x++){
  95.                 tab2[w][x]-=tab2[y][x]*mnoznik;
  96.             }
  97.         }
  98.     }
  99.     for(int y=n-1; y>=0; y--){
  100.         for(int w=y-1; w>=0; w--){
  101.             tab2[w][n]-=tab2[y][n]*tab2[w][y];
  102.             tab2[w][y]=0;
  103.         }
  104.     }
  105.     cout << endl;
  106.     double roznica=0;
  107.     for(int i=0; i<n; i++){
  108.         cout << tab[i][n] << ' ' << abs(tab[i][n]-A[i])+1 << ' ' << abs(tab[i][n]-A[i])/A[i]+1 << endl << tab2[i][n] << ' ' << abs(tab2[i][n]-A[i])+1 << ' ' << abs(tab2[i][n]-A[i])/A[i]+1 <<endl<<endl;
  109.         //cout << tab[i][n] << ' ' << abs(tab[i][n]-A[i])+1 << ' ' << abs(tab[i][n]-A[i])/A[i]+1 << endl << tab2[i][n] << ' ' << abs(tab2[i][n]-A[i])+1 << ' ' << abs(tab2[i][n]-A[i])/A[i]+1 <<endl<< abs(tab[i][n]-A[i])/abs(tab2[i][n]-A[i]) <<endl <<endl;
  110.         roznica+=abs(tab[i][n]-A[i])/abs(tab2[i][n]-A[i]);
  111.         //cout << tab2[i][n] << ' ' << abs(tab2[i][n]-A[i])+1 << ' ' << abs(tab2[i][n]-A[i])/A[i]+1 << endl;
  112.     }
  113.     //cout << roznica/n;
  114.     return 0;
  115. }
  116.  
  117. void wypisz(double **tab, int n){
  118.     cout<<endl;
  119.     for (int i=0;i<n;i++){
  120.         for (int j=0;j<n+1;j++){
  121.             cout<<tab[i][j]<<"  ";
  122.         }
  123.         cout<<endl;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement