Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 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 = 0;
  20.    
  21.     while (j < 10) {
  22.         for (int i = 0; i < size; i++) {
  23.           arr[i] = (float) rand() / RAND_MAX;
  24.         }
  25.        
  26.         cout << "Krok " << j + 1 << ": " << endl;
  27.        
  28.         wprost(arr, size, x);
  29.         horner(arr, size, x);
  30.        
  31.         cout << endl;
  32.        
  33.         j++;
  34.     }
  35.  
  36.     return 0;
  37. }
  38.  
  39. double wprost(double arr[], int size, double x) {
  40.     clock_t start = clock();
  41.     double y = 0;
  42.    
  43.     for (int i = 0; i < size; i++) {
  44.         y += arr[i] * pow(x, i);
  45.     }
  46.    
  47.     cout << "Wprost - Wynik to: " << y << endl;
  48.     cout << "Wprost - Czas potrzebny na wykonanie obliczen to: " << float(clock() - start)/CLOCKS_PER_SEC << endl;
  49.     return y;
  50. }
  51.  
  52. double horner(double arr[], int size, double x) {
  53.     clock_t start = clock();
  54.     double y = 0;
  55.    
  56.     for (int i = size; i >= 0; i--) {
  57.         y = arr[i] + y * x;
  58.     }
  59.    
  60.     cout << "Horner - Wynik to: " << y << endl;
  61.     cout << "Horner - Czas potrzebny na wykonanie obliczen to: " << float(clock() - start)/CLOCKS_PER_SEC << endl;
  62.     return y;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement