Develop an alternative formulation for calculating the polynomial expression that avoids the need for repeated power calculation and reduces the risk of numerical instability
#include <bits/stdc++.h>
#define max 10
using namespace std;
int main()
{
int ar[max];
int i, num, power;
float x, result;
cout<<"Enter the order of the polynomial \n";
cin>>num;
cout<<"Enter the value of x \n";
cin>> x;
cout<<"Enter " <<num + 1<< "coefficients
\n";
for (i = 0; i <= num; i++)
{
cin>>ar[i];
}
result = ar[0];
for (i = 1; i <= num; i++)
{
result = result * x + ar[i];
}
power = num;
cout<<"Polynomial is: \n";
for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}
if (ar[i] > 0)
cout<<" + ";
else if (ar[i] < 0)
cout<<" - ";
else
cout<<" ";
cout<<abs(ar[i])<<"x^" <<(power--);
}
cout<<"\n Value of the polynomial = "<<result;
}
===================================================================
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6
Given polynomial is:
+ 3x^2 + 2x^1 + 6x^0
Value of the polynomial = 22
Develop an alternative formulation for calculating the polynomial expression that avoids the need for repeated power...