Question: Write a C program, named a2q2.c, for interactive polynomial evaluation. It requires: a function float horner(float x, int n, float a[]) , which computes and
Write a C program, named a2q2.c, for interactive polynomial evaluation. It requires:
- a function float horner(float x, int n, float a[]), which computes and returns the value of polynomialp(x)=a[0]xn1+a[1]xn2+...+a[n2]x1+a[n1]x0p(x)=a[0]xn1+a[1]xn2+...+a[n2]x1+a[n1]x0using Horners algorithm, and
- the main function reads polynomial coefficients from command line arguments in order of a[0] a[1] a[n-2] a[n-1], and stores them in array float a[n]. Then it prompts user for inputing value x, followed by calling the horner function to evaluate the polynomial and display the value, and repeats this process until 999 is entered.
What i Have is this please help me!
#include#include float horner(float x, int n, float a[]) { // your implementation } int main(int argc, char* args[]) { //get polynomial efficients from command line arguments if (argc <= 1) { printf("Need more than one arguments, e.g.: 1 2 3 4 "); return 0; } int i, n = argc-1; // declare float array data structure for coefficients // read command line arguments convert them to floating numbers atof(args[i+1]); // repetitive polynomial evaluation for user input of x value float x = 0.0; do { //get x value from keyboard do { printf("Please enter x value (Ctrl+C or 999 to quit): "); if (scanf("%f", &x) != 1) { printf("Invalid input "); } else { break; } // handle invalid input // while(getchar() !=' ') ; do { if (getchar() == ' ') break; } while (1); } while (1); // escape when input 999 // print the polynomial expression // use x^n to denote x raised to the n-th power // use %.1f format for floating number // print polynomial by calling horner(x, n, a) } while (1); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
