#include <iostream>
using namespace std;
double hornerExp(double a[], int n, double x);
int main()
{
int n;
double *a;
double x;
cout<<"Input the n (a0, a1, , an):\n";
cin>>n;
a = new double[n+1];
cout<<"Input a0, a1, , an\n";
for (int i = 0; i <= n; i++) {
cin>>a[i];
}
cout<<"Input the x"<<endl;
cin>>x;
cout<<"y = "<<hornerExp(a, n, x)<<endl;
delete []a;
return 0;
}
double hornerExp(double a[], int n, double x)
{
double y = 0.0;
for (int i = n; i >= 1; --i) {
y += a[i];
y *= x;
}
y += a[0];
return y;
}
using namespace std;
double hornerExp(double a[], int n, double x);
int main()
{
int n;
double *a;
double x;
cout<<"Input the n (a0, a1, , an):\n";
cin>>n;
a = new double[n+1];
cout<<"Input a0, a1, , an\n";
for (int i = 0; i <= n; i++) {
cin>>a[i];
}
cout<<"Input the x"<<endl;
cin>>x;
cout<<"y = "<<hornerExp(a, n, x)<<endl;
delete []a;
return 0;
}
double hornerExp(double a[], int n, double x)
{
double y = 0.0;
for (int i = n; i >= 1; --i) {
y += a[i];
y *= x;
}
y += a[0];
return y;
}