Advertisement
desdemona

głupie słuchawki

Apr 6th, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7. vector<double> mnozenie(vector<double> matylda, double liczba){
  8.     for(int i=0; i<matylda.size(); i++)
  9.         matylda[i] = matylda[i]*liczba;
  10.     return matylda;
  11. }
  12.  
  13. vector<double> odejmowanie(vector<double> matylda, vector<double> wladyslaw){
  14.     for(int i=0; i<matylda.size(); i++)
  15.         matylda[i] = matylda[i]-wladyslaw[i];
  16.     return matylda; //matylda - wladyslaw
  17. }
  18.  
  19. int main()
  20. {
  21.     int t = 100;
  22.     //cin >> t;
  23.     srand(time(NULL));
  24.     int liczba_rownan = 5;
  25.    
  26.  
  27.     for(int iterator=0; iterator<t; iterator++)
  28.     {
  29.        
  30.         //cin >> liczba_rownan;
  31.         vector<vector<double> > macierz;
  32.         vector<double> x;
  33.         int liczba_obiegow = 0;
  34.  
  35.         for(int i=0; i<liczba_rownan; i++)
  36.         {
  37.             x.push_back((double)(rand()%999));
  38.         }
  39.  
  40.         for(int i=0; i<liczba_rownan; i++)
  41.         {
  42.             vector<double> usagi;
  43.             double b=0;
  44.             for(int j=0; j<liczba_rownan; j++)
  45.             {
  46.                 double sailor_moon = (rand()%999);
  47.                 usagi.push_back(sailor_moon);
  48.                 b = b+sailor_moon * x[j];
  49.             }
  50.             usagi.push_back(b);
  51.             macierz.push_back(usagi);
  52.         }
  53.  
  54.         //for(int i=0; i<liczba_rownan; i++)
  55.         //{
  56.         //  vector<double> usagi;
  57.         //  for(int j=0; j<liczba_rownan+1; j++)
  58.         //  {
  59.         //      double sailor_moon;
  60.         //      cin >> sailor_moon;
  61.         //      usagi.push_back(sailor_moon);
  62.         //  }
  63.         //  macierz.push_back(usagi);
  64.         //}
  65.         vector<vector<double> > eg = macierz;
  66.         vector<double> x_eg;
  67.         for(int i=0; i<eg.size(); i++)
  68.         {
  69.             x_eg.push_back(0);
  70.         }
  71.  
  72.         for(int i=0; i<eg.size(); i++)
  73.         {
  74.             //szukamy najwiekszego w kolumnie i
  75.             //tylko ostatniej kolumny nie ruszamy
  76.             int najnaj=i;
  77.             for(int j=i; j<eg.size(); j++)
  78.             {
  79.                 if(eg[j][i] > eg[najnaj][i])
  80.                     najnaj=j;
  81.             }
  82.             eg[i].swap(eg[najnaj]);
  83.             //jej, swapniete
  84.  
  85.             //teraz jak juz wybralismy najwiekszy element
  86.             //i jest on w eg[i][i] to ten wiersz dzielimy przez ten element
  87.             //robiac w miejscu eg[i][i] jedynke
  88.             eg[i] = mnozenie(eg[i], 1/eg[i][i]);
  89.             //pieknie, mam jedynki. co teraz z nimi?
  90.             // a no trzeba wiersze odejmowac.
  91.  
  92.             for(int j=i+1; j<eg.size(); j++)
  93.             {
  94.                 //matylda-wladyslaw;
  95.                 eg[j]=odejmowanie(eg[j], mnozenie(eg[i], eg[j][i]));
  96.                 for(int k=0; k<eg.size()+1; k++)
  97.                 {
  98.                     liczba_obiegow++;
  99.                 }
  100.             }
  101.             //super, mamy macierz trojkatna
  102.         }
  103.  
  104.         cout << "\n";
  105.         for(int i=0; i<eg.size(); i++)
  106.         {
  107.             //for(int j=0; j<eg.size() + 1; j++)
  108.             //{
  109.             //  cout << eg[i][j] << " ";
  110.             //}
  111.             //cout << i;
  112.             //cout << "\n";
  113.         }
  114.  
  115.         x_eg.back() = eg.back().back();
  116.         //ostatniego x-a znamy juz bez kombinowania
  117.         for(int i=eg.size()-2; i>=0; i--)
  118.         {
  119.             //;
  120.             x_eg[i] = eg[i][eg.size()];
  121.             //dajemy mu wartosc b, bedziemy od niej odejmowac
  122.             for(int j=eg.size()-1; j>i; j--)
  123.             {
  124.                 x_eg[i] -= x_eg[j] * eg[i][j];
  125.             }
  126.         }
  127.  
  128.     //  cout << "\n";
  129.     //  for(int i=0; i<eg.size(); i++)
  130.     //  {
  131.     //      for(int j=0; j<eg.size() + 1; j++)
  132.     //      {
  133.     //          cout << eg[i][j] << " ";
  134.     //      }
  135.     //      cout << "\n";
  136.     //  }
  137.     //  cout << "\n";
  138.     //  for(int i=0; i<x_eg.size(); i++)
  139.     //      cout << x_eg[i] << " ";
  140.     // 
  141.     //  cout << "\n";
  142.         double  eg_sr= 0;
  143.         double x_sr=0;
  144.         for(int i=0; i<x.size(); i++)
  145.         {
  146.             eg_sr += abs(x_eg[i]);
  147.             x_sr += abs(x[i]);
  148.         }
  149.         eg_sr = eg_sr/x.size();
  150.         x_sr=x_sr/x.size();
  151.  
  152.         double bb= abs(eg_sr - x_sr);
  153.         double bw = bb/x_sr;
  154.  
  155.        
  156.         //cout << "blad bezwzgledny " << bb << "\n";
  157.         //cout << "blad wzgledny " << bw << "\n\n";
  158.         cout << bw;
  159.         //if(bb == 0 || bw == 0)
  160.         //{
  161.         //  iterator = 0;
  162.         //}
  163.         //cout << liczba_rownan << "\t"
  164.         //cout << liczba_obiegow; //<< "\n";
  165.         liczba_rownan+=5;
  166.     }
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement