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