fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // Funkcja przygotowująca wielomian (stopień i współczynniki)
  7. void przygotujWielomian(int &stopien, vector<int> &a) {
  8. stopien = 3;
  9.  
  10. // współczynniki od a3 do a0
  11. a.push_back(1); // a3
  12. a.push_back(2); // a2
  13. a.push_back(3); // a1
  14. a.push_back(4); // a0
  15. }
  16.  
  17. // Funkcja obliczająca wartość wielomianu schematem Hornera
  18. int horner(const vector<int> &a, int stopien, int x) {
  19. int wynik = a[0]; // a3
  20.  
  21. for (int i = 1; i <= stopien; i++) {
  22. wynik = wynik * x + a[i];
  23. }
  24.  
  25. return wynik;
  26. }
  27.  
  28. int main() {
  29. int stopien;
  30. vector<int> a;
  31.  
  32. przygotujWielomian(stopien, a);
  33.  
  34. int x = 2;
  35. int W = horner(a, stopien, x);
  36.  
  37. cout << "Stopien wielomianu: " << stopien << endl;
  38. cout << "a3 = " << a[0] << endl;
  39. cout << "a2 = " << a[1] << endl;
  40. cout << "a1 = " << a[2] << endl;
  41. cout << "a0 = " << a[3] << endl;
  42. cout << "x = " << x << endl;
  43. cout << "W(" << x << ") = " << W << endl;
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Stopien wielomianu: 3
a3 = 1
a2 = 2
a1 = 3
a0 = 4
x = 2
W(2) = 26