///G. Hagopian
///Use Horner's method to evaluate a polynomial

#include <iostream>
#include <vector>
using namespace std;

void get_coeff(vector<int>& v, int deg) {
    int c;
    for(int i = 0; i <= deg; ++i) {
        cin >> c;
        v.push_back(c);
    }
}

int poly(vector<int> v, int x) {
    ///Use Horner's method to evaluate the polynomial
    int accumulator = v[0];
    for(int i = 1; i < v.size(); ++i) {
        accumulator *= x;
        accumulator += v[i];
    }
    return accumulator;
}

int main() {
    int deg;
    int x;
    vector <int> coefficients;
    cout << "\nEnter the degree of a polynomial: ";
    cin >> deg;
    cout << "\nEnter the coefficients in descending powers: ";
    get_coeff(coefficients, deg);
    cout << "\nThe coefficients are: ";
    for(int i = 0; i < coefficients.size(); ++i)
        cout << coefficients[i] << " ";
    cout << endl;
    cout<<"\nEnter the input to the polynomial: ";
    while(cin>>x) {
        cout << "\np(" << x << ") = " << poly(coefficients, x);
        cout<<"\nEnter the input to the polynomial: ";
    }
}
