Advertisement
Nakumas

Srargorytmy

Nov 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <iomanip>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. long double horner(double A[], int n, int x)
  11. {
  12.     double wartosc = 0;
  13.     if(n==0)
  14.         return A[0];
  15.     return x * horner(A, n-1, x) + A[n];
  16. }
  17.  
  18. double naPiechote(double A[], int n, int x)
  19. {
  20.     double wynik = A[0];
  21.     for(int i=1; i<=n; i++)
  22.         wynik *= x + A[i];
  23.     return wynik;
  24. }
  25.  
  26. int main()
  27. {
  28.     printf("----- Obliczanie wartosci wielomianu -----\n\n");
  29.     srand(time(NULL));
  30.     clock_t start, stop;
  31.     double czas;
  32.  
  33.     double *A;
  34.     double wynik;
  35.     int n, arg, cykl = 0;
  36.     do
  37.     {
  38.         printf("[Cykl %i] Podaj stopien:",cykl+1);
  39.         cin >> n;
  40.         A = new double [n + 1];
  41.  
  42.         for (int i = 0; i < n; i++)
  43.         {
  44.             *(A + i) = ((double) rand() / (double) RAND_MAX);
  45.             printf("%.10f ", *(A + i));
  46.         }
  47.         printf("\n");
  48.         printf("[Cykl %i] Podaj argument:",cykl+1);
  49.         cin >> arg;
  50.  
  51.         printf("# Algorytm Hornera #\n");
  52.         start = clock();
  53.         wynik = horner(A, n, arg);
  54.         printf("W(%i) = %e\n", cykl+1, wynik);
  55.         stop = clock();
  56.         czas = double(stop - start) / CLOCKS_PER_SEC;
  57.         printf("Czas: %.5f\n\n", czas);
  58.  
  59.         printf("# Algorytm na piechote #\n");
  60.         start = clock();
  61.         wynik = naPiechote(A, n, arg);
  62.         printf("W(%i) = %e\n", cykl+1, wynik);
  63.         stop = clock();
  64.         czas = double(stop - start) / CLOCKS_PER_SEC;
  65.         printf("Czas: %.5f\n", czas);
  66.  
  67.         cykl++;
  68.     } while(cykl<10);
  69.  
  70.     delete [] A;
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement