Write a recursive function for the following To evaluate a p
Write a recursive function for the following: To evaluate a polynomial of degree n (use a vector) at a value m using Horner\'s method.
Solution
#include <iostream>
using namespace std;
int horner(int poly[], int n, int x)
{
if(n==0)
return poly[n];
// Evaluate value of polynomial using Horner\'s method
return horner(poly,n-1,x)*x+poly[n];
}
int main()
{
int poly[] = {2, -6, 2, -1};
int x = 3;
int n=3;
cout << \"Value of polynomial is \" << horner(poly, n, x);
int poly1[] = {2, 0, 3, 1};
int x1 = 2;
int n1=3;
cout<<\"\ \";
cout << \"Value of polynomial is \" << horner(poly1, n1, x1);
return 0;
}
// returns value of poly[0]x(n-1) + poly[1]x(n-2) + .. + poly[n-1]
