Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cmath>
  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. srand(time(NULL));
  15.  
  16. cout << "Podaj rozmiar tablicy" << endl;
  17. cin >> size;
  18. double arr[size];
  19.  
  20. for (int i = 0; i < size; i++) {
  21. arr[i] = (float) rand() / RAND_MAX;
  22. }
  23.  
  24. wprost(arr, size, x);
  25. horner(arr, size, x);
  26.  
  27. return 0;
  28. }
  29.  
  30. double wprost(double arr[], int size, double x) {
  31. double y = 0;
  32. int a = 0;
  33. int m = 0;
  34.  
  35. for (int i = 0; i < size; i++) {
  36. y += arr[i] * pow(x, i);
  37. a++;
  38. m++;
  39. }
  40.  
  41. cout << "Wprost - Liczba dodawan: " << a << endl;
  42. cout << "Wprost - Liczba mnozen: " << m << endl;
  43. return y;
  44. }
  45.  
  46. double horner(double arr[], int size, double x) {
  47. double y = 0;
  48. int a = 0;
  49. int m = 0;
  50.  
  51. for (int i = size; i >= 0; i--) {
  52. y = arr[i] + y * x;
  53. a++;
  54. m++;
  55. }
  56.  
  57. cout << "Horner - Liczba dodawan: " << a << endl;
  58. cout << "Horner - Liczba mnozen: " << m << endl;
  59. return y;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement