Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program in C++ Code structure is provided, follow instructions. Explain step-by-step and how 0 polynomial is represented How can you represent 'a dense univariate polynomial'
Program in C++
Code structure is provided, follow instructions.
Explain step-by-step and how 0 polynomial is represented
How can you represent 'a dense univariate polynomial' with integer coefficients so that we can add them and multiply them? We do not want a numerical solution: we want to manipulate the polynomials symbolically. We want to read the values of the coefficients and the degree of the polynomial at run time and then create and manipulate the polynomials read. The degree of the polynomial p in x is the exponent of the x in the term with the highest exponent. The main program in univariate interacts with the user - Reads two univariate polynomials and outputs them. - Adds the two univariate polynomials and outputs their sum. - Multiplies two univariate polynomials and outputs their product. - Destroys them. - Continues reading/adding/multiplying until the user wants to stop. We could create the univariate polynomial p=27x+4x3 which is of degree 3 as follows int d=3; int* C; // create a dynamic array of coefficients C= new int [d+1]; C[0]=2; C[1]=7 C[2]=0; C[3]=4; // now create the univariate structure dynamically and assign the fields Univariate *p; p = new Univariate; p->deg = d; p->Cfs = C; IMPLEMENT IN UNIVARIATE.CPP THE GIVEN FUNCTION (OPERATOR) PROTOTYPES Use dynamic arrays for the coefficients so that they work for polynomials of any degree. To input the polynomial p=27x+4x3 we first read the degree 3 and store it. We now know how big to make the array of coefficients (an array of 4 elements). Subsequently, the coefficients are read in (error free) increasing degree of x 2704 In your first version that prints the univariate polynomial (with the overloaded operator for output), the univariate could look like 2x0+7x1+0x2+4x3 where we have used the character for powers (exponents). Try to improve the output (for BONUS points) so that it becomes, for instance, 27x1+4x3 You will probably find programming add more difficult than times. There are two difficulties with addition. One happens when the two polynomials have different degrees. The other is when the coefficient(s) of highest degree cancel as in the last example below. If after the addition, the resulting polynomial has a highest degree term with a zero coefficient (because of cancellation), you need to allocate a new array of the right (and smaller) size and fill in the smaller size array properly (remembering that there is a zero univariate polynomial which is a special case). Do not forget to deallocate the array that is no longer needed. Do not just "patch" the output operator to handle highest degree terms of 0. Every polynomial (except for the zero polynomial) must always have a non-zero highest degree term. Because of this restriction of having a non-zero highest degree term, you will also have to think of how you are going to represent the zero univariate polynomial, namely, the univariate polynomial that consists of the constant 0 . Your program should be tested on the following pairs of polynomials a=1+2x+3x2+4x3b=1+x+x2+x3a=1+x+x2b=1+x+x2+x3+x4a=0b=1+x+x2a=1+2x+3x2b=12x3x2 For each pair of univariate polynomials, you should read - The degree of the first polynomial - The coefficients of the first polynomial - The degree of the second polynomial - The coefficients of the second polynomial and print them out. Then print out their sum and product. And finally destroy (delete) them when no longer needed. Allow your program to continue reading pairs of polynomials until the user wants to stop. Use the data structure given in univariate.cpp. Do not make U\univariate a C++ class
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