Assignment 3: Polynomials (100 pts)
Due Wednesday, February 10 at midnight on Catalyst

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 assignment 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

        Polynomial(const Polynomial &polynomial);
        //Copy constructor. Initializes the Polynomial to have the same elements as another Polynomial


        /**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

        Polynomial operator + (const Polynomial& p);
        //Adds existing Polynomial to p, returning the result

        Polynomial operator * (const Polynomial& p);
        //Multiplies existing Polynomial by p, returning the resulting Polynomial
        //Worth 15 points extra credit

        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 + 2x3 + 7x2 + 6x1 + 1x0
P2: 3.30x2 + 9x1 + 3x0
P1 + P2: 3.50x4 + 2x3 + 10x2 + 15x1 + 4x0

  • 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).


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.


Extra Credit:

  • You will receive 15 points extra credit for correctly overloading the * operator for a polynomial.
  • All other functions and operators in the header file are mandatory.
  • If you had troubles with your List from assignment 2, you may also see me during office hours with your updated list. I will run my tests again and will give you up to 10 points back on your List assignment if all functions are now working. This offer expires on the due date for this assignment.


What To Submit:

  • When you are finished, upload your List.h, Term.h, Polynomial.h, Polynomial.cpp and PolynomialTest.cpp (This file should contain all of YOUR tests, not mine. I know what my test file looks like. I want to see YOURS here.) to Catalyst before the deadline.
  • No late work will be accepted, so please pay attention to the deadline and ask me if there is a problem.


Grading

  • I will be testing your Polynomial class functions with my own tests.
  • You will receive 100 points if all tests pass, 75 points if 3/4 of the tests pass, etc + any extra credit you may have completed for this assignment.


Sample Test File

  • I will be using this test file to evaluate your code.
  • However, I want to see YOUR OWN tests inside of PolynomialTest.cpp.
  • You should approach this assignment by writing a function and then testing it inside of YOUR PolynomialTest.cpp file.
  • Once you are certain your functions are working properly according to your own tests, only then should you run this test file on your code.
  • Writing YOUR OWN tests as you go along is one of the best strategies for avoiding bugs and is a fundamental part of programming. (I cannot emphasize this point enough - as you can see!)

/*
 * TestFile.cpp

 * @author Jennifer Parrish

 */

#include <iostream>
#include <iomanip>
#include <fstream>
#include "Polynomial.h"
using namespace std;

int main() {
    Polynomial poly;
        poly.insertTerm(4, 3.5);
        poly.insertTerm(3, 2.1);
        poly.insertTerm(2, 6.5);
        poly.insertTerm(1, 5.5);
        poly.insertTerm(0, 1);
        cout << "Printing P1. \nShould be 3.5x4 + 2.1x3 + 6.5x2 + 5.5x1 + 1x0: \n";
        poly.print();
        cout << endl << endl;
        Polynomial poly2;
        poly2.insertTerm(2, 3.3);
        poly2.insertTerm(1, 9.0);
        poly2.insertTerm(0,3.4);
        cout << "Printing P2. \nShould be 3.3x2 + 9.0x1 + 3.4x0\n";
        poly2.print();
        cout << endl << endl;
        Polynomial p_add = poly + poly2;
        cout << "Printing P3 = P1 + P2. \nShould be 3.5x4 + 2.1x3 + 9.8x2 + 14.5x1 + 4.4x0\n";
        p_add.print();
        cout << endl << endl;
        Polynomial p4;
        p4.insertTerm(5, 2.3);
        p4.insertTerm(4, 0);
        p4.insertTerm(3, 8.9);
        p4.insertTerm(2, 6.5);
        p4.insertTerm(1, 1.6);
        p4.insertTerm(0, 9.6);
        cout << "Printing P4. \nShould be 2.3x5 + 0x4 + 8.9x3 + 6.5x2 + 1.6x1 +9.6x0\n";
        p4.print();
        cout << endl << endl;
        Polynomial p5;
        p5.insertTerm(3, 3.1);
        p5.insertTerm(2, 3.1);
        p5.insertTerm(1, 3.1);
        p5.insertTerm(0, 3.1);
        cout << "Printing P5. \nShould be 3.1x3 + 3.1x2 + 3.1x1 + 3.1x0\n";
        p5.print();
        cout << endl << endl;
        Polynomial p6 = p4 + p5;
        cout << "Printing P6 = P4 + P5. \nShould be 2.3x5 + 0x4 + 12.0x3 + 9.6x2 + 4.7x1 + 12.7x0\n";
        p6.print();
        cout << endl << endl;
        Polynomial p7(p5);
        cout << "Printing copy of P5. \nShould be 3.1x3 + 3.1x2 + 3.1x1 + 3.1x0\n";
        p7.print();
        cout << endl << endl;
        cout << "Evaluating p2 at 2.0. Should print 34.6: " << fixed << setprecision(1) << poly2.evaluate(2);

}