Answer to Question #102854 in C++ for ankit singh

Question #102854
as input, you are given an integer n, a double x, followed by n+1 doubles a_n, a_{n-1}, ..., a_0. You are to print the value of the polynomial a_0 + a_1x+a_2x^2+...+a_nx^n. Here is the manual algorithm. At the beginning you just have read a_n. Next you read a_{n-1} and calculate a_nx+a_{n-1}. Next you read a_{n-2} and calculate (a_nx+a_{n-1})x+a_{n-2}. So after n iterations you will have the value of the polynomial above.
1
Expert's answer
2020-02-14T06:14:07-0500
#include <iostream>
using namespace std;

int main()
{
  int n;
  double x, a, p;
 
  cin >> n;
  cin >> x;
 
  cin >> a;
  p = a;
  for (int i=n; i>0; i--) {
    cin >> a;
    p *= x;
    p += a;
  }
 
  cout << "The value of the polynom is " << p << endl;
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS