Question
A polynomial can be represented through its coefficients. For example, the 4th degree polynomial, 3.5 x^4 + 7.2 x^2 + 6x + 5 can be
A polynomial can be represented through its coefficients. For example, the 4th degree polynomial, 3.5 x^4 + 7.2 x^2 + 6x + 5 can be represented by the array [5.,6.,7.2,0,3.5]. Since the degree of the polynomial is not known in advance, a dynamic array is more appropriate than a static array.
Task 0: Write a function that inputs the coefficients of a polynomial starting at the highest term. Make the function input the degree of the polynomial and the coefficients, using the following dialog (boldface indicates what the user types): Enter polynomial degree: 4 Enter coefficient of term 4: 3.5 Enter coefficient of term 3: 0 Enter coefficient of term 2: 7.2 Enter coefficient of term 1: 6 Enter coefficient of term 0: 5 Additionally, the user can enter a end-of-file character (^D on Linux) at any time if all remaining terms are 0. You are not required to do do input validation on this (though you would if this were not to be done in the lab period). Make your function create a dynamic array initialized to the input polynomial and return that array. Recall that C does not allow functions to return arrays though they may return a pointer, and an array is just a pointer. Also, you need to return the dimension of the array, and the only way to do that is through a pass by reference parameter. So, an appropriate prototype is: double * getPoly(int & degree);
Task 1: Write a function that evaluates a polynomial for a given value of x, using the prototype: double eval(double * poly, int degree, double x); You may use the pow function defined in the cmath library if you wish.
Task 2: First, an important property of polynomials p1 and p2 where p1 has degree greater than p2 and all coefficients are non-negative: There exists some x0 such that p1(x)>p2(x) for all x>x0 Although we won't determine this x0 in this lab, we wish to find an x0 that satisfies the first part of the property. Write a main function that does the following: Input two polynomials, p1 and p2. You may have preconditions that p1 and p2 have different degrees, and that all coefficients are non-negative (since its a precondition, you dont need to handle this as an error case) Determine a positive integral x0 such that p1(x0)>p2(x0) (or vice versa). Note that the above property guarantees that such an x0 exists. Output the x0 using the message: "A positive integral x0 such that p1(x0)>p2(x0) is " (or vice versa if p2 is bigger). Delete any dynamic data before returning, to avoid creating garbage. Note that this is considered good practice even though most operating systems will do this automatically when executing return() or exit() (since the C++ standard does not require this cleanup). You may use any helper functions you want.
Task 3: Although you may have deleted dynamic data, the data may still be in the heap. To test this, try to access the deleted data by evaluating the corresponding polynomial. State in comments what happens.
Task 4: Just for fun, make a bounds error in the above program by accessing p1[k] for various values of k that are bigger than the polynomial's degree. Explain what happens in comments.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started