Question: WRITE A C PROGRAM: TEMPLATE: #include #include // array coefs has (n+1) elements double poly(double x, int n, double coefs[n+1]) { double ttl = 0;

WRITE A C PROGRAM:

WRITE A C PROGRAM: TEMPLATE: #include #include // array coefs has (n+1)elements double poly(double x, int n, double coefs[n+1]) { double ttl =0; // TODO return ttl; } // read n+1 doubles into coefs.// remember array coefs has (n+1) elements // read the doulbe into

TEMPLATE:

#include

#include

// array coefs has (n+1) elements

double poly(double x, int n, double coefs[n+1])

{

double ttl = 0;

// TODO

return ttl;

}

// read n+1 doubles into coefs.

// remember array coefs has (n+1) elements

// read the doulbe into a temporary variable first.

void readPoly(int degree, double coefs[degree+1])

{

double c;

// Write a loop that reads a double (into c) and copies

// c into array coefs[0], coefs[1], and so on.

// Study the code in main() to learn how to read a double.

// TODO

}

int main(int argc,char* argv[])

{

int degree = 0;

int readOK;

readOK = scanf("%d", &degree);

while (readOK && degree >= 0) {

double coefs[degree+1];

readPoly(degree, coefs);

double x;

printf("x = ");

readOK = scanf("%lf", &x);

if (readOK) {

double value = poly(x, degree, coefs);

printf("P(%lf)=%lf ", x, value);

readOK = scanf("%d", &degree);

}

}

return 0;

}

Exercise 1. Polynomial evaluation (30 points) A univariate polynomial rl- re is a mathematical expression involving the sum of powers of a variable r multiplied by constant coefficients. Each term in the above sum is called a monomial, ai, i-0, - n are called the coe cents of the polynomial. and n, the largest power n with non-zero coefficient, is called the degree. A simple representation of a polynomial uses an array for storing the coefficients. Complete the provided code (polyHorner.c) by implementing functions that returns the value of a given polynomial for a given value of x. Your code should implement Horner's rule, which reduces the number of necessary multiplications by factoring out powers of x as follows ao + x (ai +r (a2 +x (...r (an-i +ran)...))) The main function repeatedly perform the following tasks: 1) read the degree of the polynomial n, 2) invokes a convenience function, which you must author, to read n + 1 coefficients from the standard input (starting from ao), 3) reads the value of r, 4) calls a function to evaluate the polynomia, and 5) print out the value of the polynomial When the user enters a negative degree, it signifies the end of the execution and no further evaluation For example, the session shown below asks the program to evaluate the following three polynomials: P(x) = 1.5 degree 0 polynomial evaluated at x = 10 P(z) = 1.0 + 2.0 ..r degree 1 polynomial evaluated at 2-3.0 P(x) =-10+ 2.0 . x + 4.0 . x2 degree 2 polynomial evaluated at x = 2.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!