Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10.  
  11. double horner(double a[], int n, int x){
  12. double v =0;
  13.  
  14. for(int i =n; i>=0;i--){
  15. v = a[i]+v*(double)x;
  16.  
  17. }
  18. return v;
  19. }
  20.  
  21.  
  22. double horner_recznie(double a[], int n, int x){
  23. double wynik =0;
  24. unsigned t0 = clock();
  25. for(int i =n; i>=0; i--){
  26. wynik = wynik + a[i]*pow(x,i);
  27. }
  28. unsigned czas = clock()-t0;
  29.  
  30. return wynik;
  31.  
  32. }
  33.  
  34. int main()
  35. {
  36. srand (time(NULL));
  37. cout<<"Podaj rozmiar tablicy :";
  38. int r;
  39. cin>>r;
  40. double a[r];
  41. for(int c =0; c<11; c++){
  42. for(int i =0; i<r; i++){
  43. double random = (double)rand() /(double)RAND_MAX;
  44. a[i] = random;
  45. }
  46.  
  47. int x = rand()%r;
  48. cout.precision(25);
  49. clock_t p;
  50. double czas;
  51.  
  52. p = clock();
  53. double h1 = horner(a, r-1, x);
  54. p = clock() - p;
  55. czas = ((double)p)/CLOCKS_PER_SEC;
  56. cout<<"czas: "<<czas<<endl;
  57. double h2 = horner_recznie(a, r-1, x);
  58. cout<<"horner wynik :"<<h1<<endl;
  59. cout<<"horner recznie wynik : "<<h2<<endl;
  60. /* if(h1-h2 != 0) cout<<"rozne"<<endl;
  61. cout<<endl;*/
  62.  
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement