Question: 2) Consider the polynomial in a single variable x whose operations include the following: degree() //Returns the degree of a polynomial. coefficient(power) //Returns the coefficient
2) Consider the polynomial in a single variable x whose operations include the following:
degree() //Returns the degree of a polynomial.
coefficient(power) //Returns the coefficient of the xpower term.
chageCoefficient(newCoefficient, power) //Replaces the coefficient of the xpower term with newCoefficient.
For this problem, consider only polynomials whose exponents are nonnegative integers. For example,
p = 4x5 + 7x3 x2 + 9
The following examples demonstrate the operations on this polynomial.
p.degree() is 5 (the highest power of a term with a nonzero coefficient)
p.coeficient(3) is 7 (the coefficient of the x3 term)
p.coefficient(4) is 0 (the coefficient of a missing term is implicitly 0)
p.chengeCoefficient(-3, 7) produces the polynomial
p = -3x7 + 4x5 + 7x3 x2 + 9
Using these operations, write statements to perform the following tasks:
a) Display the coefficient of the term that has the highest power.
b) Increase the coefficient of the x3 term by 8.
c) Compute the sum of two polynomials. (p and q)
d) Consider a sparse implementation of the polynomial that stores only the terms with nonzero coefficients. Complete this sparse implementation.
e) Define a traverse operation for the sparse implementation of a polynomial that will allow you to add two sparse polynomials without having to consider terms with zero coefficients explicitly.
Test Data for a) and b): p = -3x2 + 6x9 + 7x3 8x7 + 9 - 2x4
Test Data for c): p = -3x2 + 6x9 + 7x3 8x7 + 9 - 2x4 q = 9x3 - 2x8 + 4x3 + 6x + 2 + 7x4
Test Data for d) and e): p = -6x10 + 8x25 + 2x9 4x3 + 9x + 5x4
q = 43x24 - 7x10 + 4x25 + 12x + 2 + 3x4 + + 9x9
Requirements: 1) Use these test data above for your program.
2) Please turn both your source code and the output.
Note: Use screen print to capture the output according to the specified test data.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
