Sample Assignment: The Polynomial Class with a Dynamic Array


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.
poly1.cxx:
The implementation file for the new
polynomial class.
polytest1.cxx:
A simple interactive test program.
polyexam1.cxx:
A noninteractive test program that will
be used to grade the correctness of your polynomial class.
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:
main_savitch_4
, and
change the include statement to include poly1.h
.
poly1.h
:
private: double *coef; // Pointer to the dynamic array unsigned int size; // Current size of the dynamic array unsigned int current_degree;
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).
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(p1.degree( ) + p2.degree( ) + 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.
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
.