fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // Funkcja obliczająca wartość wielomianu schematem Hornera
  7. double horner(const vector<double>& a, int stopien, double x)
  8. {
  9. double wynik = a[0];
  10. for (int i = 1; i <= stopien; i++)
  11. {
  12. wynik = wynik * x + a[i];
  13. }
  14. return wynik;
  15. }
  16.  
  17. // Funkcja wypisująca dane i wynik
  18. void wypisz(int stopien, const vector<double>& a, double x, double wynik)
  19. {
  20. cout << "stopien wielomianu: " << stopien << endl;
  21. cout << "a3 = " << a[0] << endl;
  22. cout << "a2 = " << a[1] << endl;
  23. cout << "a1 = " << a[2] << endl;
  24. cout << "a0 = " << a[3] << endl;
  25. cout << "x = " << x << endl;
  26. cout << "w(3) = " << wynik << endl;
  27. }
  28.  
  29. int main()
  30. {
  31. int stopien = 3;
  32. vector<double> a = {2, 3, 4, 5}; // a3, a2, a1, a0
  33. double x = 3;
  34.  
  35. double wynik = horner(a, stopien, x);
  36. wypisz(stopien, a, x, wynik);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
stopien wielomianu: 3
a3 = 2
a2 = 3
a1 = 4
a0 = 5
x = 3
w(3) = 98