CPS 100 FALL 1996: Assignment #2

Due: Monday, Sept. 30 by 8am

Last Date to Turn in: Monday, Oct. 7 by 8am

40 points

In your cps100 directory, create a directory called assign2 using the mkdir command. Change into the assign2 directory.

In order to do this assignment, you need to copy some files using the following cp command (don't forget the trailing period, or dot):

       cp  ~rodger/cps100/assign2/*  .

This command will copy files into your directory for you to use. If you type ls you should see the following files: Makefile, polynomial.cc, polynomial.h, and testpoly.cc.

For the programming problem that follows, you should use the style rules discussed in class, which includes meaningful variable names, indentation, and comments (pre and postconditions) at the top of the file and for each function. Also include your name, date, course, and purpose in a comment at the top of the program.

Problem: A Polynomial Class

Design a class called Polynomial that can store a polynomial with real coefficients. The polynomial should be implemented using a linked list. Your polynomial should be sparse, that is, if the coefficient is zero for a particular term, then there should not be a node for that term.

Your class should have the following.

  1. Use a linked list to store the coefficients of the polynomial.

  2. A constructor with no arguments to create the zero polynomial: 0.

  3. A constructor with three arguments that specify the coefficients of the 2cd, 1st, and 0th terms. For example, if the coefficients are 3.4, 0, and 8, the polynomial is 3.4x^2 + 8.

  4. A constructor with one argument to create a polynomial with a 0th term.

  5. A constructor with two arguments to create a polynomial with a 1st and 0th term

  6. A copy constructor.

  7. A destructor. Nodes should be returned to the memory heap.

  8. Degree() - returns the degree of the polynomial

  9. SetCoefficient(term, coeff) - changes the coefficient of a particular term to coeff. For example if term=7 and coeff=8.1, this changes the x^7 coefficient to 8.1. Should return the value true (1) if successful. This may involve creating a new term if term 7 does not exist or removing a term that is now 0.

  10. ComputePoly(x) - return the value of the polynomial evaluated with x set to a particular value. For example, if the polynomial is x^3 + x + 3 and the value of x is 2, return 2^3 + 2 + 3 = 13.

  11. PrintPoly - print the polynomial in the form below. Don't print terms with a zero coefficient (unless the polynomial is just 0). This function should not return a value.

        3.5 * x^7 + 4.5 * x^2 + 8
    

  12. RetrieveCoeff(term) - Return the coefficient of a particular term. For the polynomial above, if term = 2, then return 4.5.

  13. AddByPoly(P) - Add the polynomial P to this polynomial. Return true (1) if successful, 0 otherwise.

  14. PrintRevPoly() - Print the polynomial in reverse order (from smallest term to largest term). This function must use recursion.

  15. In addition, you must have at least one useful private function that is used by a public function.

You must also write a program called testpoly.cc that shows a thorough testing of your polynomial class. It should demonstrate that every part of your class works.

In attacking this program, first design your program on paper, and come ask questions if you are stuck. When you start the implemention, don't try to type in the whole program all at once, but rather add functions one at a time, make sure they work, then add more functions. Starting early will allow you to ask more questions.

File setup and style

For this assignment, you are to create a C++ class for polynomials in the two files polynomial.cc and polynomial.h, a program to test the class called testpoly.cc, and a file called README. Your program should follow the style rules, using meaningful variable names, indentation, and comments that include your full name, date, and a comment with each function you write. Your program should be modular, that is, containing many functions, each with a different purpose. The README file is worth 2 points and should contain your name, date, an estimate of how long you worked on the programs, and a list of people you consulted with in doing this assignment.

A Makefile is given. It does not need to be modified. To compile your program using the Makefile, just type: make testpoly

Input/Output

There is no input for this program. Your program testpoly.cc should not read in any input.

Print meaningful output.

Extra Credit (4 points)

Add an additional public member function called MultByPoly.

MultByPoly(P) - multiply the polynomial by another polynomial P. Return true (1) if the multiplication was successful, 0 otherwise.

Show this function works in your testpoly.cc program.

Submitting Program

When your programs compile and produce the correct output, create a "README" file (please use all capital letters). Include your name, section number, the date, and an estimate of how long you worked on the assignment in the "README" file. You must also include a list of names of all those people (students, prof, tas, tutor) with whom you consulted on the assignment. See the rules for collaboration in the CPS 100 syllabus.

To submit your programs electronically type:

    submit100 assign2 README polynomial.cc polynomial.h testpoly.cc

You should receive a message telling you that the program was submitted correctly. If it doesn't work try typing ~rodger/bin/submit100 in place of submit100 above.