When calculating the value of a polynomial function fx anxn
Solution
P(x) = AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0
This can be re written as:-
P(x) = A0 + X(A1 + X(A2 + X(A3 + X(Q4 + X(...X(An-1 + XAn)))), and evaluation starting from the inner loop.
The c code is:-
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
void main()
{
int arr[MAXSIZE],i, number, pow;
float x, psum;
printf(\"Enter order of the polynomial \ \");
scanf(\"%d\", &number);
printf(\"Enter the value of x \ \");
scanf(\"%f\", &x);
/* Reading the coefficients into an arr */
printf(\"Enter %d coefficients \ \", number + 1);
for (i = 0; i <= number; i++)
{
scanf(\"%d\", &arr[i]);
}
psum = arr[0];
for (i = 1; i <= number; i++)
{
psum = psum * x + arr[i];
}
pow = number;
printf(\"The polynomial is: \ \");
for (i = 0; i <= number; i++)
{
if (pow < 0)
{
break;
}
if (arr[i] > 0)
printf(\" + \");
else if (arr[i] < 0)
printf(\" - \");
else
printf(\" \");
printf(\"%dx^%d \", abs(arr[i]), pow--);
}
printf(\"\ Sum of the polynomial is = %6.2f \ \", psum);
}
