Lab 8: Polynomial Part 1 (100 pts)

Due Tuesday, May 31 at 1:00pm on Catalyst and as a hard copy in class at 1:30pm

Polynomial Review
  • Recall polynomials from your math classes.
  • A polynomial is an expression consisting of variables and coefficients.
  • For example, consider the following polynomial

8.5x2 + 4.9x + 1.6

  • In this example, 8.5, 4.9 and 1.6 are all coefficients.
  • 2, 1, and 0 (which are typically not shown) are all exponents.
  • 8.5x2, 4.9x and 1.6 are all called terms. Terms are the combination of a coefficient and an exponent.


Polynomial Operations: Add
  • We can perform a variety of operations on polynomials.
  • For example, we can add two polynomials together, like this:

   8.5x2 + 4.9x + 1.6

+ 4.5x3 + 2.6x + 3.3
4.5x3 + 8.5x2 + 7.5x + 4.9

  • Notice what happened above. The coefficients of the terms with the same power of exponent were added together (aka, adding like terms). Any terms that had a different exponent from all other terms were not added together.


Polynomial Operations: Multiply

  • When we multiply polynomials, we must multiply each term together and then add the like terms. For example:

(x + 1) (4x2 + 2x + 1) = 4x3 + 2x2 + x + 4x2 + 2x + 1 = 4x3 + 6x2 + 3x + 1


Evaluating Polynomials

  • We can evaluate a polynomial by plugging in a value for x. For example if we want to evaluate the polynomial 8.5x2 + 4.9x + 1.6 for x = 2, we would perform the following calculations:

8.5(2)2 + 4.9(2) + 1.6 = 83.65


Representing Polynomials in this Assignment
  • In math, we are allowed to ignore any term with a 0 coefficient. For example, in math, we would write the polynomial 2x2 + 0x + 1 as 2x2 + 1.
  • For the purposes of this assignment, however, we will include the 0 coefficients when storing the terms of our polynomial and when printing it to the console.
  • Likewise, in math, it is acceptable not to write the exponents 0 and 1 as part of the term. For example, in math, we would write the polynomial
    2x2 + 0x1 + 1x0 as 2x2 + 1.
  • For the purposes of this assignment, however, we will include the 0 and 1 exponents when we store our terms and when printing the polynomial.

The Term Class

  • In this assignment, we will be representing a polynomial using a list of Term objects.
  • I have provided you with the Term class below.
  • Note that the Term class has private coefficient and exponent fields.
  • It also has two public constructors and some public accessors and manipulation procedures for the coefficient and exponent.


#ifndef TERM_H_
#define TERM_H_

using namespace std;

class Term
{
    private:
        double coefficient;
        int exponent;
    public:
        Term(): coefficient(0.0), exponent(0) {};
        Term(double newCoefficient, int newExponent): coefficient(newCoefficient), exponent(newExponent){};
        double getCoefficient() { return coefficient; }
        int getExponent() { return exponent; }
        void setCoefficient(double c) { coefficient = c; }
        void setExponent(int e) { exponent = e; }

};


#endif /* TERM_H_ */

The Polynomial Class
  • We will be representing our polynomial as a List of term objects.
  • Below is the header file for the Polynomial class.
  • Note that the only field is the private List<Term> poly.
  • You must use your doubly-linked and templated List from Lab 2 for this List poly.
#include "List.h"
#include "Term.h"
#include <iostream>
#include <cstddef> //for NULL
using namespace std;

class Polynomial
{
    private:

        List<Term> poly;

    public:
         /**Constructors and Destructors*/

        Polynomial();
        //Default constructor; initializes an empty Polynomial

        ~Polynomial();
        //Destructor. Frees memory allocated to the list

        /**Manipulation Procedures*/

        void insertTerm(int exponent, double coefficient);
        //inserts a single term into the polynomial

        /**Additional Operations*/

        double evaluateTerm(Term t, double value);
        //A helper function for evaluate.
        //Evaluates a single term in the Polynomial by plugging in the value
        //For example: If the term is 3.0x2 and the value is 5.0
        //This function will return 75.0 since 3.0*5.0*5.0=75.0

        double evaluate(double value);
        //Evaluates the Polynomial by plugging in the value at all terms
        //If the Polynomial is empty, returns 0.0.
        //Calls the evaluateTerm function on each term in the Polynomial

        void print();
        //Prints the Polynomial to the console in the form of
        //<coefficient1>x<exponent1> + <coefficient2>x<exponenent2> + ...
        //For example: 2.5x4 + 3x3 + 0x2 + 8.1x1 +7.5x0
};


  • Your task is to implement the above functions in a file called Polynomial.cpp (do not implement them in the header file) and test them in a file called PolynomialTest.cpp.
  • You will need to test each of the functions as you write it to ensure that it is working properly.


A Word About Printing

  • We will not be attempting to use superscript for any of the exponents.
  • Also, we will be printing out exponents that are 0 and 1 and any term with a 0 coefficient.
  • Therefore, below is an example of how your polynomials should be printed:

P1: 3.50x4 + 2.0x3 + 7.0x2 + 6.0x1 + 1.0x0
P2: 3.30x2 + 9.0x1 + 3.0x0
P1 + P2: 3.50x4 + 2.0x3 + 10.0x2 + 15.0x1 + 4.0x0

  • Note that there is an x in the middle of each term. This is something that you will add as part of your print function for Polynomial.
  • Important Note: You should also store the 0 term coefficients in your polynomial as well as any 0 and 1 exponents. This will make your life easier when you need to add (and multiply).
  • Each coefficient should be printed to one decimal value.
  • Also, do not print a trailing + sign with no term after it, or you will lose 6 points for this function.


Clarification of Insert_Term

  • You may assume that all coefficients are inserted in decreasing order of exponent.
  • Hint: you should thus be able to write this function using a single line of code.


What To Submit:

  • When you are finished, upload your List.h, Polynomial.cpp and PolynomialTest.cpp (This file should contain all of YOUR tests to ensure your functions are working properly) to Catalyst before the deadline. Also please print these files and bring them to class.
  • It is assumed that your Term.h and Polynomial.h are unaltered from what are provided. Therefore, you do not need to give them to me.

How You Will Be Graded:

  • You will receive 12 points for each correct function and 28 points for a COMPLETE test file (all functions tested more than once).