Horner scheme P50036


Statement
 

pdf   zip   main.cc

html

Let p[0… n] be a vector of integer numbers that contains the coefficients of a polynomial of degree n≥0. For instance, the vector p=⟨3,2,5,−1⟩ represents p(x)=3+2x+5x2x3, a polynomial of degree n=3.

Write a function

int evaluate(const vector<int>& p, int x);

that evaluates the polynomial at the point x, that is, that returns ∑i=0n p[i]xi.

Use the Horner scheme:

pnxn+pn−1xn−1+…+p0=((pnx+pn−1)x+…)x+p0.

Observation You only need to submit the required procedure; your main program will be ignored.

Information
Author
Jordi Petit
Language
English
Translator
Carlos Molina
Original language
Catalan
Other languages
Catalan
Official solutions
C++
User solutions
C++