Discrete Mathematics for Computer Science

(Romina) #1
Chapter Review 585

(b)

INPUT: n E N, the coefficients of P stored in a[0.. n], and a real number x0
OUTPUT: P(xo)

Poly = a [0]
fori =Oton
X =X0
for j = 1 to i
X = X "X0
Poly = Poly + a[i]. x
print Poly

(c) The code shown implements Homer's algorithm.

at x --

INPUT: n E N, the coefficients of P stored in a [0 .. n], and a real number x0
OUTPUT: P(xo)

Poly = xo " a[n] + a[n - 1]


/ down to means to subtract 1 each time through the loop until i <0 0

for i = n - 2 down to 0


Poly = Poly .xo + a[i]

print Poly

Free download pdf