Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include <time.h>
- #include <windows.h>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- long double horner(double A[], int n, int x)
- {
- double wartosc = 0;
- if(n==0)
- return A[0];
- return x * horner(A, n-1, x) + A[n];
- }
- double naPiechote(double A[], int n, int x)
- {
- double wynik = A[0];
- for(int i=1; i<=n; i++)
- wynik *= x + A[i];
- return wynik;
- }
- int main()
- {
- printf("----- Obliczanie wartosci wielomianu -----\n\n");
- srand(time(NULL));
- clock_t start, stop;
- double czas;
- double *A;
- double wynik;
- int n, arg, cykl = 0;
- do
- {
- printf("[Cykl %i] Podaj stopien:",cykl+1);
- cin >> n;
- A = new double [n + 1];
- for (int i = 0; i < n; i++)
- {
- *(A + i) = ((double) rand() / (double) RAND_MAX);
- printf("%.10f ", *(A + i));
- }
- printf("\n");
- printf("[Cykl %i] Podaj argument:",cykl+1);
- cin >> arg;
- printf("# Algorytm Hornera #\n");
- start = clock();
- wynik = horner(A, n, arg);
- printf("W(%i) = %e\n", cykl+1, wynik);
- stop = clock();
- czas = double(stop - start) / CLOCKS_PER_SEC;
- printf("Czas: %.5f\n\n", czas);
- printf("# Algorytm na piechote #\n");
- start = clock();
- wynik = naPiechote(A, n, arg);
- printf("W(%i) = %e\n", cykl+1, wynik);
- stop = clock();
- czas = double(stop - start) / CLOCKS_PER_SEC;
- printf("Czas: %.5f\n", czas);
- cykl++;
- } while(cykl<10);
- delete [] A;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement