Consider the ADT polynomial-in a single variable x-whose operations include the
ID: 3887312 • Letter: C
Question
Consider the ADT polynomial-in a single variable x-whose operations include the following: +degree):integer squeryy Returns the degree of a polynomial. +getCoefficient(in power:integer):integer / Returns the coefficient of the xpower term. +changeCoefficient(in newCoef:integer, in power.integer) //Replaces the coefficient of the xpowet term I/ with newCoef. For this problem, consider only polynomials whose exponents are nonnegative integers. For example, p=4x5 + 7x3 . x2 + 9 The following examples demonstrate the ADT operations on this polynomial. p.degree ) is 5 (die highest power of a term with a nonzero coefficient) p getCoefficient (3) is 7 (the coefficient of the x3 term) p .getCoefficient (4) is 0 (the coefficient of a missing term is implicidy 0) p. changeCoefficient(-3, 7) produces the polynomial p=-3x7 + 4x5 + 7x3 . x2 + 9 Using only the ADT operations provided, write statements to perform the following tasks:Explanation / Answer
Given below are the answers to the question. Since the programming language is not specified, I have given both C++ and Java versions. Please use which ever is needed. Hope the answer helped. If it did, please do rate the answer . Thank you very much.
a) Display the constant term
A constant is the co-efficient of the term x^0, so we need to use the following
p.getCoefficient(0)
======================================================
b) Change each coefficient by multipling them by 5
The following code changes the co-efficients in a loop starting from constant till the
highest degree
int deg = p.degree();
for(int i = 0; i <= deg; i++)
{
p.changeCoefficient(i, 5 * p.getCoefficient(i));
}
======================================================
c)Display the polynomial
For a C++ program
int coeff;
for(int i = p.degree(); i >= 0; i--)
{
coeff = p.getCoefficient(i);
if(coeff != 0)
{
if(coeff < 0)
cout << " - " << -coeff;
else
{
if(i == p.degree()) //don't display + sign for 1st term if it s positive
cout << coeff ;
else
cout << " + " << coeff;
}
if(i != 0)
cout << "x^" << i;
}
}
For a Java program
int coeff;
for(int i = p.degree(); i >= 0; i--)
{
coeff = p.getCoefficient(i);
if(coeff != 0)
{
if(coeff < 0)
System.out.println(" - " + (-coeff));
else
{
if(i == p.degree()) //don't display + sign for 1st term if it s positive
System.out.println(coeff);
else
System.out.println(" + " + coeff);
}
if(i != 0)
System.out.println("x^" + i);
}
}
======================================================
d) Change the polynomial to its derivative
int coeff;
for(int i = 0; i <= p.degree(); i++)
{
coeff = p.getCoefficient(i);
//multiply the coeff by power and reduce the power by 1
coeff = coeff * i;
p.changeCoefficient(coeff, i - 1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.