In this task, your job is to implement basic operations on polynomials. Polynomials are stored as terms in descending order of exponents. We obviously do not store terms with zero coefficients.
For example, the polynomial \( 2x^4 - x^3 + 3x^2 - 1.5x + 1 \) will be stored in a vector as:
{{4, 2.0}, {3, -1.0}, {2, 3.0}, {1, -1.5}, {0, 1.0}}
and it should be printed by your program as:
2x^4 - x^3 + 3x^2 - 1.5x + 1
print_polynomial
: Prints a polynomial in standard format. When the exponent of a term is 1
\(x \) should be printed instead of \(x^1\). If the exponent is 0, only the coefficient should be
printed. Similarly, every positive term should have a + preceding it except if it
is the first term.
add_polynomials
: Adds two polynomials and returns the resulting polynomial. It should be possible to
implement this function by doing a single scan of the input
polynomials, as they are stored in descending order of the exponents.
mul_polynomials
: Multiplies two polynomials and returns the resulting
polynomial. It should use the function add_polynomials
to accumulate the intermediate sums.
Refer to the main
function below to see how these functions will be used and tested.