Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Abstract data type Consider an abstract data type (ADT) for polynomials (in a si

ID: 3783611 • Letter: A

Question

Abstract data type

Consider an abstract data type (ADT) for polynomials (in a single variable x) whose operations include the following: ini degree()//Returns the degree of the polynomial. int coefficient(int power)//Returns the coefficient of the x^power term. void changeCoefficient(int newCoefficient, int power)//Replaces the coefficient of the x^power term with newCoefficient For this problem, consider only polynomials whose exponents are nonnegative integers. For example, p = 4x^5 + 7x^3 - x^2 + 9 The following examples demonstrate the ADT operations on this polynomial. p.degree() is 5 (the highest power of a term with a nonzero coefficient) p.coefficient(3) is 7 (note that the coefficient of a missing term is implicitly 0) p.changeCoefficient(-3, 7) produces the polynomial p = -3x^7 + 4x^5 + 7x^3 - x^2 + 9 Using these ADT operations, write C++ statements to perform the following tasks for an arbitrary polynomial q. Display the coefficient of the term that has the highest power to the screen. Decrease the coefficient of the x^3 term by 2. Compute the sum of two polynomials q and r. Place the result in a third polynomial s that has been previously initialized to zero.

Explanation / Answer

a) #include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class poly{

size_t Poly::degree() const //which returns degree
size_t degree = 0;
for (int i = 0; i < (int)maxPoly; i++)
{
if (coeff[i] != 0)
{
degree = (size_t)i;
}
}
return degree;
}

double Poly::retrieveCoeff(size_t i) const//returns the coefficient
{
if (0 <= (int)i)
{
return coeff[i];
}
else if (i >= (int)maxPoly)
{
return 0.0;
}
else
{
throw std::out_of_range("Index out of range");
}
}


b))void Poly::decrementCoeff(double value, size_t i)//returns the cofficient less than 1
{
if (0 <= (int)i)
{
if (i < (int)maxPoly)
{
grow(i);
}
coeff[i] *= value;
}
else
{
throw std::out_of_range("Index out of range");
}

}

}

c))

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

struct poly{
int coeff;
int pow;
poly *next;
};

class add2poly
{
poly *poly1, *poly2, *poly3;
public:
add2poly()
{poly1=poly2=poly3=NULL;}
void addpoly();
void display();
};

void add2poly :: addpoly(){
int i,p;
poly *newl=NULL,*end=NULL;
cout<<"Enter highest power for x ";
cin>>p;
//Read the first polynomial
cout<<" First Polynomial ";
for(i=p;i>=0;i--)
{
newl=new poly;
newl->pow=p;
cout<<"Enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coeff;
newl->next=NULL;
if(poly1==NULL)
poly1=newl;
else
end->next=newl;
end=newl;
}

//Read the Second polynomial
cout<<" Second Polynomial ";
end=NULL;
for(i=p;i>=0;i--)
{
newl=new poly;
newl->pow=p;
cout<<"Enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coeff;
newl->next=NULL;
if(poly2==NULL)
poly2=newl;
else
end->next=newl;
end=newl;
}

//Addition of two polynomials
poly *p1=poly1,*p2=poly2;
end=NULL;
while(p1 !=NULL && p2!=NULL){
if(p1->pow == p2->pow){
newl=new poly;
newl->pow=p--;
newl->coeff=p1->coeff + p2->coeff;
newl->next=NULL;
if(poly3==NULL)
poly3=newl;
else
end->next=newl;
end=newl;
}
p1=p1->next;
p2=p2->next;
}
}

void add2poly :: display(){
poly *t=poly3;
cout<<" Answer after addition is : ";
while(t!=NULL){
cout.setf(ios::showpos);
cout<<t->coeff;
cout.unsetf(ios::showpos);
cout<<"X"<<t->pow;
t=t->next;
}
}


void main(){
clrscr();
add2poly obj;
obj.addpoly();
obj.display();
getch();
}

sample output:

enter highest power for x
4
first polynomial
enter co-efficient for degree4:: 2
enter co-efficient for degree3:: 3
enter co-efficient for degree2:: 4
enter co-efficient for degree1:: 5
enter co-efficient for degree0:: 6

second polynomial
enter co-efficient for degree4:: 2
enter co-efficient for degree3:: 3
enter co-efficient for degree2:: 4
enter co-efficient for degree1:: 5
enter co-efficient for degree0:: 6

answer after addition is:4x^4+6x^3+8x^2+10x^1+12x^0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote