# (Solved Homework): FIGURE 4-10 A sparse polynomial degree headPtr coeff power next…

Language C++

Consider a sparse implementation of the ADT polynomial that stores only the terms with nonzero coefﬁ cients. For example, you can represent the revised polynomial p in Exercise 9 of Chapter 1 with the linked chain shown in Figure 4-10.

Don't use plagiarized sources. Get Your Custom Essay on
(Solved Homework): FIGURE 4-10 A sparse polynomial degree headPtr coeff power next…
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!

a. Complete the sparse implementation.

b. Deﬁ ne a traverse operation for the ADT polynomial that will allow you to add two sparse polynomials without having to consider terms with zero coefﬁ cients explicitly.

FIGURE 4-10 A sparse polynomial degree headPtr coeff power next

Given below is an implemention of polynomial as described in the question. Also a sample program to create and add polynomials is shown. Please do rate . the answer if it helped. Thank you very much.

polynomial.h

#ifndef polynomial_h
#define polynomial_h

struct term
{
int coeff;
int power;
struct term* next;
};

class polynomial
{
private:
int degree;
public:
polynomial();
void addTerm(int coeff, int power); //stores terms in decreasing order of power
int getDegree() const;
int getCoeff(int power) const;
void display();
};

#endif /* polynomial_h */

polynomial.cpp

#include “polynomial.h”
#include <iostream>
using namespace std;
polynomial::polynomial()
{
degree = 0;
}
//adds terms in sorted decreasing order of power. If a term with same power exists, then the coeffs are added together
{
struct term* prev = 0;
while(curr != 0 )
{
if(power > curr->power)
break;
else if(curr->power == power) //already a term with given power exists, add the coeff
{
curr->coeff += coeff;
return;
}
else
{
prev = curr;
curr = curr->next;
}
}

struct term* t = new term;
t->coeff = coeff;
t->power = power;
t->next = 0;

if(prev == 0) //adding as 1st term
{
degree = power;
}
else
{
prev->next = t;
t->next = curr;
}
}
{
polynomial result;

while(poly1 != 0 && poly2 != 0)
{
if(poly1->power > poly2->power)
{
poly1 = poly1->next;
}
else if(poly2->power > poly1->power)
{
poly2 = poly2->next;
}
else
{
int sum = poly1->coeff + poly2->coeff;
if(sum != 0)
poly1 = poly1->next;
poly2 = poly2->next;
}
}

//add any left out terms from poly1 and poly2
while(poly1 != 0)
{
poly1 = poly1->next;
}

while(poly2 != 0)
{
poly2 = poly2->next;
}
return result;

}
int polynomial::getDegree() const
{
return degree;
}
int polynomial::getCoeff(int power) const
{
if(power > degree)
return 0;
while(curr != 0)
{
if(curr->power == power)
return curr->coeff;
else if(curr->power < power) //no need to search further since we have come to lower powers
break;
else
curr = curr->next;
}
return 0;
}

void polynomial::display()
{
cout << “0”;

else
{
//display 1st term
cout << curr->coeff << “x^” << curr->power ;
curr = curr->next;
while(curr != 0)
{
if(curr->coeff > 0)
cout << ” + ” << curr->coeff << “x^” << curr->power ;
else
cout << ” – ” << -(curr->coeff) << “x^” << curr->power ;

curr = curr->next;
}

}

cout << endl;
}

polymain.cpp

#include “polynomial.h”

#include <iostream>
using namespace std;
int main()
{
polynomial p1;
polynomial p2;

//p1 = 3x^4 – 2x^2 + 5

//p2 = 8x^5 + 3x^4 + x^2 -2

cout << “p1 = ” ; p1.display();
cout << “p2 = ” ; p2.display();

cout << “p3 = p1 + p2 = ” ; p3.display();

}

output

p1 = 3x^4 – 2x^2 + 5x^0
p2 = 8x^5 + 3x^4 + 1x^2 – 2x^0
p3 = p1 + p2 = 8x^5 + 6x^4 – 1x^2 + 3x^0

Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

## Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.