fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Funkcja wczytująca stopień i współczynniki wielomianu
  6. void wczytajWielomian(vector<int>& a, int& stopien) {
  7. stopien = 3; // stopień wielomianu
  8. a.resize(stopien + 1);
  9.  
  10. // współczynniki: a3=1, a2=2, a1=3, a0=4
  11. a[0] = 1; // a3
  12. a[1] = 2; // a2
  13. a[2] = 3; // a1
  14. a[3] = 4; // a0
  15. }
  16.  
  17. // Funkcja obliczająca wartość wielomianu (schemat Hornera)
  18. int obliczWartosc(const vector<int>& a, int stopien, int x) {
  19. int wynik = a[0];
  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. vector<int> a;
  30. int stopien;
  31. int x = 2;
  32.  
  33. wczytajWielomian(a, stopien);
  34. int W = obliczWartosc(a, stopien, x);
  35.  
  36. cout << "Stopien wielomianu: " << stopien << endl;
  37. cout << "a3 = " << a[0] << endl;
  38. cout << "a0 = " << a[3] << endl;
  39. cout << "x = " << x << endl;
  40. cout << "W(2) = " << W << endl;
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5264KB
stdin
Standard input is empty
stdout
Stopien wielomianu: 3
a3 = 1
a0 = 4
x = 2
W(2) = 26