Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 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.  
  9. using namespace std;
  10. double horner(double wsp[],int st, int x);
  11. double zButa(double wsp[],int st, int x);
  12. int main()
  13. {
  14. clock_t h; //zegar dla hornera
  15. clock_t p; //zegar dla pieszo
  16.  
  17. srand (time(NULL));
  18. int cykle = rand()%10+10 ;
  19. int st = 0;
  20. int x =0;
  21. cout<<"Podaj stopien wielomianu: ";
  22. cin>>st;
  23. cout<<"\nPodaj punkt ktorego wartosc chcesz znac: ";
  24. cin>>x;
  25. double wsp[st];
  26. for(int i = 0; i < cykle; i++){
  27. cout<<endl<<"cykl "<<i+1<<endl;
  28. for(int i =0 ; i <=st; i++){
  29. wsp[i] = double(rand())/(double(RAND_MAX)+1.0); //uzupełnianie tablicy w. losowymi od 0 do 1
  30. }
  31. h = clock();
  32. cout<<fixed<<setprecision(5)<<horner(wsp, st,x)<<endl;
  33. h = clock() - h;
  34. double czas_horner = ((double)h)/CLOCKS_PER_SEC;
  35. p = clock();
  36. cout<<fixed<<setprecision(5)<<zButa(wsp,st,x);
  37. p = clock() - p;
  38. double czas_pieszo = ((double)p)/CLOCKS_PER_SEC;
  39. cout<<"\nRoznica wynikow w "<<i+1<<" cyklu wynosi: "<<(horner(wsp, st,x))-(zButa(wsp,st,x))<<endl;
  40.  
  41. cout<<"Czas metody Hornera w cyklu "<<i+1<<" wynosi: "<<czas_horner<<endl;
  42. cout<<"Czas metody 'na piechote' w cyklu "<<i+1<<" wynosi: "<<czas_pieszo<<endl;
  43.  
  44. }
  45. return 0;
  46. }
  47. double horner(double wsp[],int st, int x)
  48. {
  49. double wynik = 0;
  50. for(int i=st;i>=0;i--){
  51. wynik = wynik*(double)x + wsp[i];
  52. }
  53. return wynik;
  54. }
  55. double zButa(double wsp[],int st, int x){
  56. double wynik =0;
  57. for(int i = st; i >=0; i--){
  58. wynik = wynik + wsp[i]*pow(x,i);
  59. }
  60. return wynik;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement