In: Computer Science
Using C++
1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n where n <= 10 The program inputs n, ai, and x from the keyboard and then prints out the corresponding value of p to the screen. The program continues to input new values of n, ai, x and evaluates p until a negative value for n is input, at which point the program stops.
Program
#include <iostream>
#include<cmath>
using namespace std;
//main function
int main()
{
//variable declaration
double a[11], x;
int n;
//processing
while(1)
{
//prompt and read the order of the polynomial
cout<<"Enter n: ";
cin >> n;
//check if n is negative
if(n<0) break;
//read the coefficients of the polynomial
for(int i=0; i<=n; i++)
{
cout<<"Enter a"<<i<<": ";
cin>>a[i];
}
//prompt and read the value of x
cout<<"Enter x: ";
cin>>x;
//calculate the value of the polynomial
double sum = 0;
for(int i=0; i<=n; i++)
{
sum += a[i]*pow(x, i);
}
//display the value of the polynomial
cout<<"The value of the polynomial:
"<<sum<<endl;
}
return 0;
}
Output:
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.