2 70 points A C program can represent a real polynomial px o
2. (70 points) A C program can represent a real polynomial p(x) of degree n as an array of the real
coefficients a0 a1, …, an.
p(x) = a0 + a1x + a2x2 …, an xn
Write a C program that inputs a polynomial of degree 8 and then evaluates the polynomial at variousvalues of x. The output should have two decimal digits.
Enter the eight coefficients: 1.2 3 2.4 4 1 9.1 3 0
Enter x: 4.4
Output: 37552.73
1) Name your program poly.c.
2) As part of the solution, write and call the function get_poly() with the following prototype.
The get_poly() function should fill the array of eight coefficients.
void get_poly(double *coeff);
The get_poly() function should use pointer arithmetic – not subscripting – to visit arrayelements. In other words, eliminate the loop index variables and all use of the [] operator inthe function.
3) As part of the solution, write and call the function eval_poly() with the followingprototype.
double eval_poly(double *coeff, double x);
4) You might need the pow function in math.h library to calculate the result of raising x tothe power y. The function prototype of the pow function:
double pow(double x, double y);
5) Compile the program with –lm: gcc -lm -Wall poly.c
Solution
#include <stdio.h>
void get_poly(double *coeff)
{
printf(\"Enter the eight coefficients:\");
int i;
for(i=0;i<8;i++)
{
scanf(\"%lf\",coeff+i);
}
}
double pow(double x, double y)
{
int i;
double ans=1;
for(i=0;i<y;i++)
{
ans = ans*x;
}
return ans;
}
double eval_poly(double *coeff, double x)
{
double ans=0;
int i;
for(i=0;i<8;i++)
{
ans = ans + (*(coeff+i))*pow(x,i);
}
return ans;
}
int main()
{
double coeff[8];
get_poly(coeff);
double x;
printf(\"Enter x:\");
scanf(\"%lf\",&x);
double ans = eval_poly(coeff,x);
printf(\"Output: %.2lf\ \",ans);
return 0;
}

