Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<ctime>
  4.  
  5. using namespace std;
  6.  
  7. double wprost(double arr[], int size, double x);
  8. double horner(double arr[], int size, double x);
  9.  
  10. int main(){
  11.     double x = 3.5;
  12.     int size;
  13.     cout.precision(10);
  14.    
  15.     cout << "Podaj rozmiar tablicy" << endl;
  16.     cin >> size;
  17.     double arr[size];
  18.    
  19.     int j = 5;
  20.    
  21.     while (j > 0) {
  22.         for (int i = 0; i < size; i++) {
  23.           arr[i] = (float) rand() / RAND_MAX;
  24.         }
  25.        
  26.         wprost(arr, size, x);
  27.         horner(arr, size, x);
  28.        
  29.         j--;
  30.     }
  31.  
  32.     return 0;
  33. }
  34.  
  35. double wprost(double arr[], int size, double x) {
  36.     clock_t start = clock();
  37.     double y = 0;
  38.    
  39.     for (int i = 0; i < size; i++) {
  40.         y += arr[i] * pow(x, i);
  41.     }
  42.    
  43.     cout << "Wynik to: " << y << endl;
  44.     cout << "Czas potrzebny na wykonanie obliczen to: " << float(clock() - start)/CLOCKS_PER_SEC << endl;
  45.     return y;
  46. }
  47.  
  48. double horner(double arr[], int size, double x) {
  49.     clock_t start = clock();
  50.     double y = 0;
  51.    
  52.     for (int i = size; i >= 0; i--) {
  53.         y = arr[i] + y * x;
  54.     }
  55.    
  56.     cout << "Wynik to: " << y << endl;
  57.     cout << "Czas potrzebny na wykonanie obliczen to: " << float(clock() - start)/CLOCKS_PER_SEC << endl;
  58.     return y;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement