Answered Essay: 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.

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

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

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.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?