$35.00

## Description

**The A****s****s****i****g****nm****e****n****t****:**

^{Imp}^{le}^{m}^{e}^{n}^{t} ^{a }^{p}^{o}^{l}^{y}^{n}^{o}^{m}^{i}^{al}** **^{c}^{lass t}^{h}^{at}^{ u}^{ses} ^{a}^{ d}^{ou}^{b}^{l}^{y}^{-lin}^{k}^{ed} ^{li}^{s}^{t of}

**polynodes**** **to store the **polynomia****l**‘s terms. Each **polynode**** **of the list holds the **coeffic****i****ent**** **and **exponent**** **for one term. The terms are kept in order from smallest to largest **exponen****t**. Each **polynomial**** **also maintains a pointer called **recent_ptr**** **to the most recently accessed **polynode**.

**Purp****o****s****e****s:**

^{E}^{n}^{sure} ^{t}^{h}^{at} ^{you}^{ c}^{a}^{n w}^{r}^{ite} ^{a}^{ c}^{lass t}^{h}^{at }^{u}^{ses a} ^{lin}^{k}^{ed} ^{li}^{s}^{t.}

**D****u****e ****D****a****t****e:**

^{Mon}^{d}^{ay,} ^{N}^{o}^{v}^{e}^{m}^{b}^{er} ^{4,} ^{8} ^{a}^{m}^{, via Moo}^{d}^{l}^{e}^{.}

**F****i****l****e****s t****h****at ****y****ou m****u****s****t wr****i****t****e:**

1. **poly.h**: The header file for the new **p****o****l****y****n****o****m****i****al**** **class.

^{2}^{. } ^{poly.cx}^{x}^{:} ^{T}^{h}^{e} ^{i}^{m}^{p}^{le}^{m}^{e}^{n}^{ta}^{t}^{i}^{o}^{n} ^{f}^{i}^{le} ^{f}^{o}^{r }^{th}^{e}^{ n}^{ew} ^{polynomial}** **^{c}^{las}^{s}^{.}

**Ot****h****e****r fi****l****e****s t****h****at ****y****ou**** ****may**** ****fi****n****d h****e****l****p****f****u****l****:**

1. **polytest.cx****x**: A simple interactive test program.

2. **polyexam.cx****x**: A non-interactive test program that will be used to grade the correctness of your **polynomi****a****l**** **class.

3. **Makefil****e**: compile the test files with the **polynomial**** **class.

**The**** ****P****o****lynom****i****al**** ****C****l****ass**** ****with**** ****a**** ****Link****e****d**** ****List**

^{D}^{i}^{scussi}^{o}^{n}** **^{of}** **^{t}^{h}^{e}** **^{A}^{ssign}^{m}^{e}^{n}^{t}

As indicated above, you will write the **p****o****l****y****n****o****m****i****a****l**** **class, which uses a doubly- linked list to store **coefficient****s**.

Step 1. **Polynomia****l****s**** **keep linked lists of **p****o****l****y****n****o****d****e****s**. Look at the **polynode**** **class in **p****o****l****y****.h**. This class defines a single term (**c****o****e****f****f****i****c****i****e****nt**** **plus **exponent**) in a **p****o****l****y****n****o****m****i****a****l**. All of the functions for a **polynode**** **are defined for you already in this header file.

Please also look at the **private**** **member variables for the **polynomial**** **class, also in **poly.h**:

**private:**

**//**** ****Head**** ****pointer**** ****for**** ****list**** ****of**** ****nodes polynode***** ****head_ptr;**

^{//}** **^{Tail}** **^{pointer}** **^{for}** **^{list}** **^{of}** **^{nodes}

**polynod****e********* ****tail_ptr;**

**//**** ****Pointer**** ****to**** ****most**** ****recently**** ****used**** ****node mutable**** ****polynod****e********* ****recent_ptr;**

^{//}** **^{Current}** **^{degree}** **^{of}** **^{the}** **^{polynomial}

**unsigned**** ****int**** ****current_degree;**

The meaning of the **m****u****t****a****b****le**** **keyword will be covered in class. But a brief explanation now: Our plan is to keep the **recent_p****t****r**** **always pointing to the most recently used **p****o****l****y****node**. For example, when we call the **coefficient **member function, we will move the **recent_p****t****r**** **to point to the **polynode**** **that contains the requested **exponent**. With a normal member variable, we could

not do this (since the **coefficient**** **is a **c****o****n****st**** **member function and it is forbidden from changing normal member variables). So the meaning of the **mutable**** **keyword is to indicate that changing the member variable does not change the value of the **polynomial**** **in a meaningful way (and therefore, the compiler will let **c****o****n****st**** **member functions change a **m****u****t****a****b****le**** **variable).

In your **poly.cxx**, write a clear description of how the member variables of a **polynomial**** **are used. The **head_ptr**** **and **tail_ptr**** **are the head and tail pointers for a doubly–linked list of **polynod****e****s**** **that contain the **polynomia****l**‘s

terms in order from smallest to largest **e****x****p****o****n****e****n****t**. To make certain operations simpler, we will always keep a **polynode**** **for the zero–order (x^0) term. But other **polynodes**** **are kept only if the **c****o****e****f****f****i****c****i****e****nt**** **is non-zero. We always

maintain **recent_****p****t****r**** **as a pointer to some **polynode**** **in the list—preferably the most recently used **p****o****l****y****node**. The degree of the **polynomi****a****l**** **is stored in **current_degree**** **(using zero for the case of all zero **coefficien****t****s**).

Step 2: The **poly.h**** **header file contains a prototype for a **private**** **member function that will make it easier to implement everything else:

**//**** ****A**** ****private**** ****member**** ****function**** ****to**** ****aid**** ****the**** ****other**** ****functions:**

**void**** ****set_recent(unsigned**** ****int**** ****exponent)**** ****const;**

The **set_recent**** **function will set the **r****e****c****e****n****t****_****p****t****r**** **to the **p****o****l****y****node**** **that contains the requested **exponent**. If no such **exponent**** **exists, then **recent_ptr**** **should be set to the last **pol****y****node**** **that is still less than the specified **exponent**. Note that **s****e****t****_****r****e****c****e****n****t**** **is a **const**** **member function, but that it can still change the **mutable**** ****r****e****c****e****n****t****_****p****t****r**** **member variable. My implementation of **set_recent**** **used four cases:

If the requested **e****x****p****o****n****e****n****t**** **is zero, then set **recent_ptr**** **to the head of the list.

Else if the **exponent**** **is greater than or equal to the **current****_****degre****e**, then set

^{recent_ptr}** **^{to }^{th}^{e tail of t}^{h}^{e} ^{li}^{s}^{t.}

Else if the **exponent**** **is smaller than the **e****x****p****o****n****e****n****t**** **in the recent **pol****y****node**, then move the **r****e****c****e****n****t****_****p****t****r**** **backward as far as needed.

Else move the **r****e****c****e****n****t****_****p****t****r**** **forward as far as needed.

Step 3. Implement the constructors, destructor, and assignment operator. For the default constructor, you could start by creating a valid empty

^{polynomial}** **^{(}^{w}^{i}^{th} ^{a} ^{h}^{ead} ^{polynode}** **^{t}^{h}^{a}^{t}^{ h}^{as }^{exponent}** **^{a}^{n}^{d }^{coefficient}** **^{of}

^{z}^{e}^{r}^{o}^{)}^{.}

Then call **assign_****c****oef**** **to set the one term. (Note that you need to make a 2–

**polynode**** **list if the **exponent**** **here is greater than 0.)

The copy constructor should also start by creating a valid empty **polynomia****l**. Then have a loop that steps through the terms of the **source**** **(using **source.next_ter****m**). Each term of the **source**** **is placed in the **polynomial **(using **assign_co****e****f**).

The assignment operator first checks for a self-assignment, then clears out the terms (using the **c****l****e****ar**** **function). Finally, it has a loop (similar to the copy constructor) to copy the terms of the **so****u****rc****e**.

Step 4. Implement the **a****s****s****i****g****n****_****c****o****e****f**** **function. After **calling**

**set_recent(exponent)**, my implementation had these cases:

If there is a zero **c****o****e****f****f****i****c****i****e****nt**** **and **e****x****p****o****n****e****nt**** **greater than **c****u****r****r****e****n****t****_****degree**, return with no further work.

Else if there is currently no **polynode**** **for the given **exponent**** **(so that **recent_ptr->exponent****(****)**** **is less than the new **exponent**), make a new **polynode**** **and connect it into the list.

Else if the **coeffic****i****ent**** **is non-zero or the **exponent**** **is zero, just change the

^{coefficient}** **^{of an exi}^{s}^{ti}^{n}^{g }^{polynode}** **^{in} ^{t}^{h}^{e} ^{li}^{s}^{t.}

Else if the **exponent**** **equals the **c****u****r****r****e****n****t****_****d****e****g****r****e****e**, the **c****o****e****f****f****i****c****i****e****nt**** **is zero (otherwise we would have hit the previous case), so we remove the tail **polynode**, set the **r****e****c****e****n****t****_****p****t****r**** **to the new tail, and reduce the **current_degree**.

Else, in this last case, we must have a new zero **coefficient**** **for a **polynode **that is neither the head nor the tail. We just remove this **polynode**** **and set the **recent_ptr**** **to the *pre**v**ious** ***polynode**** **in the list.

Step 5. Implement the **ad****d****_****t****o****_coef**** **function, using logic like what you used in step 4.

Step 6. Write **next_term**** **and **previous_term**** **so that they each start with **set_recent****.**

Hint for **next_term**: Start by calling **s****e****t****_****r****e****c****e****n****t****(****e****x****p****o****n****e****n****t****)**. Normally, the right answer is then in the **polynode**** **after **recent_ptr**** **(but there is one exception that you should handle).

Hint for **previous_ter****m**: Start by checking the special case where **exponent**** **is zero. Then call **set_recent(exponent**** ****–**** ****1)**. Normally, the right answer is then in the **r****e****c****e****n****t****_****p****t****r**** ****polynode**** **(but again, there is one special case that you need to handle–the case where the **pol****y****node**** **has **exponent**** **and

**coefficient**** **of zero).

Step 7. When these above functions are working, use them to write the **operator**** ****+**, **o****p****e****r****a****t****or**** ****–**, and **o****p****e****r****a****t****o****r**** ********* **functions to compute the sum, difference, and product of two **polynomia****l****s**. Write the **d****e****r****i****v****a****t****i****v****e****(****) **function to compute the derivative of a **polynomia****l**, using the power rule. And write **eval**** **(and its sibling function, **o****p****e****r****a****t****o****r****()**** **) to evaluate a **polynomial**** **at a particular value of x.

For 5% extra credit, you may write the root-finding function **f****i****n****d****_****r****o****o****t**, which uses Newton’s method to locate a value of x that makes the **p****o****l****y****n****o****m****i****al **(evaluated at x) equal to zero.