Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <ctime>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- double horner(double wsp[],int st, int x);
- double zButa(double wsp[],int st, int x);
- int main()
- {
- clock_t h; //zegar dla hornera
- clock_t p; //zegar dla pieszo
- srand (time(NULL));
- int cykle = rand()%10+10 ;
- int st = 0;
- int x =0;
- cout<<"Podaj stopien wielomianu: ";
- cin>>st;
- cout<<"\nPodaj punkt ktorego wartosc chcesz znac: ";
- cin>>x;
- double wsp[st];
- for(int i = 0; i < cykle; i++){
- cout<<endl<<"cykl "<<i+1<<endl;
- for(int i =0 ; i <=st; i++){
- wsp[i] = double(rand())/(double(RAND_MAX)+1.0); //uzupełnianie tablicy w. losowymi od 0 do 1
- }
- h = clock();
- cout<<fixed<<setprecision(5)<<horner(wsp, st,x)<<endl;
- h = clock() - h;
- double czas_horner = ((double)h)/CLOCKS_PER_SEC;
- p = clock();
- cout<<fixed<<setprecision(5)<<zButa(wsp,st,x);
- p = clock() - p;
- double czas_pieszo = ((double)p)/CLOCKS_PER_SEC;
- cout<<"\nRoznica wynikow w "<<i+1<<" cyklu wynosi: "<<(horner(wsp, st,x))-(zButa(wsp,st,x))<<endl;
- cout<<"Czas metody Hornera w cyklu "<<i+1<<" wynosi: "<<czas_horner<<endl;
- cout<<"Czas metody 'na piechote' w cyklu "<<i+1<<" wynosi: "<<czas_pieszo<<endl;
- }
- return 0;
- }
- double horner(double wsp[],int st, int x)
- {
- double wynik = 0;
- for(int i=st;i>=0;i--){
- wynik = wynik*(double)x + wsp[i];
- }
- return wynik;
- }
- double zButa(double wsp[],int st, int x){
- double wynik =0;
- for(int i = st; i >=0; i--){
- wynik = wynik + wsp[i]*pow(x,i);
- }
- return wynik;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement