Lab 9: Polynomial Part 2 (100 pts)

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

Additional Polynomial Operations:
  • For this lab, we will be adding to the Polynomial class from Lab 8.
  • Add the following prototypes to your Polynomial.h file:

/**Public functions*/

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

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

Polynomial operator - (const Polynomial& p);
//Subtracts p from this Polynomial, returning the result, a third Polynomial

Polynomial square();
//Multiplies the existing Polynomial by itself, returning a new Polynomial

Polynomial distribute(double coefficient, int exponent);
//Multiplies all of the terms of this Polynomial by the Term,
//whose coefficient and exponent are given as parameters

/**Private helper functions*/
Term addTerm(Term t1, Term t2);
//A helper function for the + function and the - function
//Adds two Terms together and returns the result as another Term

Term multiplyTerm(Term t1, Term t2);
//A helper function for the the square and distribute functions
//Multiplies two terms together and returns the result as another Term




Polynomial Operations: Add and Subtract
  • 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.
  • The same approach is used to subtract, except the terms are subtracted instead of added together


Polynomial Operations: Multiply and Square

  • 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 

  • When we square a polynomial, the operation is similar, as we are multiplying a polynomial by itself:

(4x2 + 2x + 1) * (4x2 + 2x + 1)

= (4x2 * 4x2) + (2x * 2x) + (1 * 1) + 2 * (4x2 * 2x) + 2 * (4x2 * 1) + 2* (2x * 1)

= 16x4 + 16x3+ 12x2 + 4x + 1



Polynomial Operations: Distribute

  • Distributing here refers to multiplying a single term by all the terms in a polynomial.
  • For example, below we are distributing the term 3x5 through the polynomial 4x2 + 2x + 1
  • We must thus multiply 3x5 by all the terms in the polynomial, as show below:

    (3x5) * (4x2 + 2x + 1) = 12x7 + 6x6 + 3x5


Sample Test File

  • I will be using a test file similar to this one 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 + 0.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 + 0.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();
        Polynomial p8 = p4 - p5;
       
cout << "Printing P8 = P4 - P5. \nShould be 2.3x5 + 0.0x4 + 5.8x3 + 3.4x2 + -1.5x1 + 6.5x0\n";
        p8.print();
  
        Polynomial p9 = poly2.square();
        cout << "Printing P9 = Square of P2. \nShould be 10.9x4 + 59.4x3 + 103.4x2 + 61.2x1 + 11.6x0\n";
        p9.print();
 
        Polynomial p10 = p5.distribute(4.2, 4);
        
cout << "Printing P10 = P5 * 4.2x4." << endl;
        cout <<"Should be
13.0x7 + 13.0x6 + 13.0x5 + 13.0x4 + 0.0x3 + 0.0x2 + 0.0x1 +0.0x0\n";
        p10.print();
  
 
    
        cout << endl << endl;
        cout << "Evaluating p2 at 2.0. Should print 34.6: " << fixed << setprecision(1) << poly2.evaluate(2);

}


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, Polynomial.h and TestFile.cpp are unaltered from what are provided. Therefore, you do not need to give them to me.

How You Will Be Graded:

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