(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 coeffi 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!
Order Essay

a. Complete the sparse implementation.

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

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

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

Expert Answer

 

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:
struct term* headPtr;
int degree;
public:
polynomial();
void addTerm(int coeff, int power); //stores terms in decreasing order of power
polynomial addPolynomial(const polynomial &other);
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()
{
headPtr = 0;
degree = 0;
}
//adds terms in sorted decreasing order of power. If a term with same power exists, then the coeffs are added together
void polynomial::addTerm(int coeff, int power)
{
struct term* prev = 0;
struct term* curr = headPtr;
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;
t->next = headPtr;
headPtr = t;
}
else
{
prev->next = t;
t->next = curr;
}
}
polynomial polynomial::addPolynomial(const polynomial &other)
{
polynomial result;
struct term* poly1 = headPtr;
struct term* poly2 = other.headPtr;

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

//add any left out terms from poly1 and poly2
while(poly1 != 0)
{
result.addTerm(poly1->coeff, poly1->power);
poly1 = poly1->next;
}

while(poly2 != 0)
{
result.addTerm(poly2->coeff, poly2->power);
poly2 = poly2->next;
}
return result;

}
int polynomial::getDegree() const
{
return degree;
}
int polynomial::getCoeff(int power) const
{
struct term* curr = headPtr;
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()
{
struct term* curr = headPtr;
if(headPtr == 0)
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
p1.addTerm(3, 4);
p1.addTerm(-2,2);
p1.addTerm(5, 0);

//p2 = 8x^5 + 3x^4 + x^2 -2
p2.addTerm(8, 5);
p2.addTerm(3, 4);
p2.addTerm(1, 2);
p2.addTerm(-2, 0);

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

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

Homework Ocean
Calculate your paper price
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.

Professional and Experienced Academic Writers

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.

24/7 Customer Support

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?

Follow these simple steps to get your paper done

Place your order

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

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

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.

Essays

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.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents