Sample Assignment: The Polynomial Class with a Dynamic Array
(Chapter 4)

Data Structures and Other Objects Using C++
Third Edition
by Michael Main and Walter Savitch
ISBN 0-321-19716-X

The Assignment:
Implement a polynomial class that uses a dynamic array to store the polynomial's coefficients.
Ensure that you can write a class that uses a dynamic array.
Before Starting:
Read Sections 4.1-4.3 and 4.6.
Complete the previous assignment of a polynomial that uses a fixed array to store its coefficients ( , and make sure that you have followed all the recommended clean-up suggestions ( .
Due Date:
Tuesday, Oct 3. If you run into problems, you may submit the work on Wednesday with no penalty. You may also submit on Thursday or Friday with a penalty of 5 points per day. No submissions allowed after Friday.
Files that you must write:
  1. poly1.h: The header file for the new polynomial class. Start with our our version of poly1.h and add your name and email address at the top.
  2. poly1.cxx: The implementation file for the new polynomial class.
Other files that you may find helpful:
  1. polytest1.cxx: A simple interactive test program.
  2. polyexam1.cxx: A non-interactive test program that will be used to grade the correctness of your polynomial class.

The Polynomial Class with a Dynamic Array
Discussion of the Assignment

As indicated above, you will revise the polynomial class to use a dynamic array. You'll need to copy your poly0.cxx to a new file poly1.cxx and make these changes:

  1. Change the namespace to main_savitch_4, and change the include statement to include poly1.h.

  2. Delete the CAPACITY and MAX_EX constants. You may replace these with a constant called DEFAULT_CAPACITY which is used by the constructor to determine the initial size of the dynamic array.

  3. Look at the new private member variables in poly1.h:
            double *coef;      // Pointer to the dynamic array
            unsigned int size; // Current size of the dynamic array
            unsigned int current_degree;
  4. Add a new reserve member function. The purpose of this function is the same as the bag's reserve function in Chapter 4 (making sure that the size of the dynamic array is at least equal to the specified number). However, be careful that you understand how your reserve function may need to differ from the bag's reserve function. For example, the newly allocated array for the polynomial must always be at least the degree plus one. Also, in the case of the bag, the new part of the larger array did not need to be initialized because it was not being used (but depending on how your coefficient function is implemented, your polynomial might expect the unused part of the array to contain zeros).

  5. Modify the other polynomial member functions to use the dynamic array. These functions should always work correctly, even if the programmer now tries to set a term with an exponent that is beyond the current size of the dynamic array. Note: The only functions that need to be modified are those functions that access your member variables directly. In my implementation, this included the constructor, add_to_coef, assign_coef, clear, coefficient, and the precondition check in operator *. By the way, the change made in my operator * was to replace the precondition check with the statement:
        answer.reserve( ) + ) + 1);
    This statement is not absolutely needed, but it does make the computation more efficient by correctly reserving the right amount of room from the start. You might make similar calls to the reserve function in any other function that calculates and returns a polynomial.

  6. Implement a copy constructor, an assignment operator and a destructor. Be careful that you do not just copy the bag's implementations for these functions. You must understand enough about your polynomial class to see what features differ from the bag.

  7. Add one new function, the find_root function that was discussed in class. (It already has documentation in the new poly1.h file.) You should be able to write this function with no direct access to the member variables. In my implementation, I found it useful to use the <cmath>function fabs(b) to compute the absolute value of a number b.

Michael Main (