a C program can represent a real polynomial p(x) of degree n as an array of the
ID: 3635546 • Letter: A
Question
a C program can represent a real polynomial p(x) of degree n as an array of the real coefficients a0,a1,......,an where an does not equal to 0
p(x)= a0+a1x1+a2x2+......+anxn
write a program that inputs a polynomial of maximum degree 8 and then evaluates the polynomial at various values of x. Include a function get_poly that fills the array of coefficients and sets the degree of the polynomial, and a function eval_poly that evaluates a polynomial at a given value of x. Use these function prototypes:
void get_poly( double coeff[], int* degreep );
double eval_poly( const double coeff[], int degree, ( double x );
Explanation / Answer
Hi) Here you got: Before I post all the code I want to explain it a bit: I have implemented both functions that are in the task and one more, with prototype: double power(double x, int pow) This function computes x in power of pow. I used it when implemented evaluating function. The main Idea is that at first you declare an array of 9 elements of double and declare a pointer to it. Then declare integer variable that represents a power of our polynomial and declare a pointer to this variable. Then pass those pointer to get_poly function and read degree and coefficients of polynomial. Then I read the value at which user wants to evaluate that polynomial and evaluate it with eval_poly function. I made the function call inside the printf, but if you want you can declare another variable and store the result of eval_poly function in it like this: double result = eval_poly(coeff,degree,x); So enjoy the code and write back if you did not understand anything ;) CODE: #include #include double power(double x, int pow) { double result=1; if (pow == 0) return 1; else { int i = 0; for (i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.