Hello all,

I hope I find the help I need here. I am sorry I am not good enough in C++ yet. This is my first year so please be patient with me.

This is my problem:

p = 4x^5 + 7x^3 - x^2 +9

the ADT operations are:

p.degree() is 5 ( the highest power of a term with a nonzero coefficient )
p.coefficient (3) is 7 ( the coefficient of the x^3 term )
p.coefficient (4) is 0 ( the coeffient of a missing term is implicitly 0 )
p.ChangeCoefficient (-3, 7) produces the polynomial
p = -3x^7 + 4x^5 + 7x^3 - x^2 +9

so the question is : Using these ADT operations, write statements to perform the following tasks:

a. Display the coefficient of the term that has the highest power.
b. Increase the coefficient of the x^3 term by 8.
c. Compute the sum of two polynomials.

My answers so far:

a.

cout<< "The coefficient of the term that has the highest power is: " << p.degree();

b.

cout<< "The new coefficient of x^3 is: " << p.changeCoefficient(7+8, 3);

c. I couldn't get this one. I am a little bit confused.


Thanks for your help in advance

Dani AI

Generated

A compact, correct set of statements and a few cautions that build on 's attempts and 's sum idea.

To display the coefficient of the highest-power term, query the degree and then ask for that coefficient (handle the zero-polynomial case first):

int d = p.degree();
if (d < 0) // depends on your ADT: some use -1 for the zero polynomial
    cout << "zero polynomial";
else
    cout << p.coefficient(d) << endl;

To increase the coefficient of the x^3 term by 8, read the current coefficient and write back the increased value:

int k = 3;
int cur = p.coefficient(k);
p.ChangeCoefficient(cur + 8, k);

To compute the sum of two polynomials, loop over all exponents from 0 up to the larger degree, add corresponding coefficients, and set the result. Skip storing explicit zeros if the ADT treats missing terms as zero:

Polynomial result;
int maxdeg = std::max(a.degree(), b.degree());
for (int i = 0; i <= maxdeg; ++i) {
    int s = a.coefficient(i) + b.coefficient(i);
    if (s != 0) result.ChangeCoefficient(s, i);
}

Notes and cautions: check how your ADT defines the degree of the zero polynomial (some return -1, some 0). Confirm the exact method names/case used in your implementation (coefficient vs Coefficient, ChangeCoefficient vs changeCoefficient). If the ADT leaves trailing zero coefficients, remove them or ensure the ADT updates its internal degree after changes so degree() stays correct.

Recommended Answers

All 2 Replies

Say you have for example polynomials a and b.

Polynomial sums are calculated by summing the coefficients of the same power terms. So first you have to figure out how many terms the polynomials have and work your way down from there using a loop.

maxdegree = max(a.degree() , b.degree() )

You can store the results in a new polynomial object or a/b

result.changeCoefficient( a.coefficient(maxdegree)+b.coefficient(maxdegree) , maxdegree )

Hope this helped

thanks, but what about section a and b am I true or not?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.