Question

In: Computer Science

Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an...

Skills needed to complete this assignment: dynamic arrays, classes.

PROMPT:

In mathematics, a polynomial is an expression consisting of cariables and coefficients which involves only the operation of addition, subtraction, multiplication, and non-negative integer exponents of variables. A polynomial in a single variable can always be written in the form: anxn + an-1xn-1+ ....... + a2x2 + a1x + a0

Where a0 ....., an are coefficients and x is the variable.

In this assignment, you will complete a polynomial class using dynamic arrays. For simplicity, we only consider polynomials with one variable and integer coefficients.
One simple way to implement the polynomial class is to use an array of integers to store the coefficients. The index of the array is the exponent of the corresponding term. If a term is missing, then it simply has a zero coefficient. For example, we can use [2, 0, 0, 4] to represent the polynomial 4x3 + 2, since the polynomial can be rewritten as 2x0 + 0x1 + 0x2 + 4x3 .
The Poly class provided in the template has two data members, an integer called arraySize, an integer pointer called coeff.coeff will be used to link the dynamic coefficient array, and arraySize will be used to hold the current size of coeff array. With these data members, you will complete the following class member functions and friend functions.

Poly()

The default class constructor. A class member function. It is used to allocated an array of size DEFAULTPOLY (a global constant provided in the template) and initialize the polynomial to the constant 0.

Poly(int size)

An alternate class constructor. A class member function. It is used to allocate an array of size size (the input parameter) and initialize the polynomial to the constant 0.

Poly(const Poly& aPoly)

The copy constructor. A class member function. It is used to construct a new Poly that is a copy of an existing polynomial object. aPoly. Note that the new Poly object does not share the same dynamic array with aPoly. A new array needs to be allocated to it.

~Poly()

The deconstructor. A class member function. It is used to destroy a Poly object by freeing its dynamically allocated array.

void grow(int newSize)

A class member function. If newSize is greater than the current array size, this function will increase the size of the dynamically allocated array by allocating a new array of the desired size, copying the data from the old array to the new array, and then releasing the old array. If newSize is less than or equal to the current size, no actions are taken.

int degree() const

A class member function. The degree of a single variable polynomial is the highest exponent of its terms with non zero coefficients. For example, the polynomial 4x3 + 2 has degree 3, and the polynomial 2 has degree 0. This function returns the degree of the polynomial.

void setCoeff(int value, int i)

A class member function. The function is used to add a term value x xi, in the polynomial. Grow the coefficient array if necessary.

int getCoeff(int i) const

A class member function This function is used to find the coefficient of the xiterm in the polynomial. If the polynomial does not contain a term with power i (for example i >= arraySize) , the function returns 0.

void negate()

A class member function. The polynomial will be changed to represent its multiplication by -1 after this function is called. For example, the polynomial 4x3 + 2 will be changed to -4x3 -2

Poly operator+ (const Poly& aPoly, const Poly& bPoly)

A friend function of the Poly class. This function overloads the operator +. This function adds two polynomials and returns a new polynomial that is the result. For example, with poly1 = 4x3 + 2 and poly2 = 5x3 +2x + 2, poly1 + poly2 is 9x3 + 2x + 4

Poly operator- (const Poly& aPoly, const Poly& bPoly)

A friend function of the Poly class. This function overloads the operator -. This function subtracts the second parameter Poly object from the first parameter Poly object and returns a new polynomial that is the result. For example, with poly1 = 4x3 + 2 and poly2 = 5x3 + 2x + 2, poly1 - poly2 is -x3-2x.

(Hint: poly1 - poly2 = poly1 + (-1) * poly2)

bool operator== (const Poly& aPoly, const Poly& bPoly)

A friend function of the Poly class. This function overloads the operator ==. It is used to decide whether two Poly objects represent the same polynomial. Note that for two Poly objects representing the same polynomial, their coeff array sizes can be different. For example, coefficient arrays [2, 0, 0, 4] and [2, 0, 0, 4, 0, 0] both represent the polynomial 4x3+2

ostream& operator<< (ostream& out, const Poly &aPoly)

A friend function of the Poly class. This function overloads the operator <<. It prints a Poly object to the given output stream in a readable format. For example, the polynomial 4x3-2x+2 will be printed as 4x^3 - 2x^1+2. Note that we will not print the variable nor exponent for a term with zero exponent (2x0 is printed as 2). If a polynomial has no nonzero coefficient, it is printed as 0.

PROGRAM TEMPLE: YOU MUST DEVELOP YOUR CODE FROM THIS TEMPLATE. DO NOT MODIFY THE CLASS HEADER.

#include <cmath>

#include <iostream>

using namespace std;

const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array

//DO NOT modify the class header

class Poly

{

private:

//Data members

int arraySize; //size of array

int *coeff; //dynamic array

public:

Poly();

Poly(int size);

Poly(const Poly& aPoly);

~Poly();

const Poly& operator=(const Poly& aPolt);

void grow(int newSize);

int degree() const;

void setCoeff(int value, int i);

int getCoeff(int i) const;

void negate();

friend Poly operator+(const Poly& aPoly, const Poly& bPoly);

friend Poly operator-(const Poly& aPoly, const Poly& bPoly);

friend bool operator==(const Poly& aPoly, const Poly& bPoly);

friend ostream& operator<<(ostream& out, const Poly &aPoly);

};

int main() {

Poly poly1, poly2;

int numCoeff, coeffValue, coeffDegree, x;

//prompt user for number of coefficients

cout << "How many coefficients for polynomial 1?" << endl;

cin >> numCoeff;

for(int i=0; i<numCoeff; ++i)

{

cout << "Coefficient " << i+1 << " for polynomial 1:";

cin >> coeffValue >> coeffDegree;

poly1.setCoeff(coeffValue, coeffDegree);

}

cout << endl << "How many coefficients for polynomial 2?" << endl;

cin >> numCoeff;

for(int i=0; i<numCoeff; ++i)

{

cout << "Coefficient " << i+1 << " for polynomial 2:";

cin >> coeffValue >> coeffDegree;

poly2.setCoeff(coeffValue, coeffDegree);

}

//sample test cases for degree and operator <<

cout << endl << "Polynomial 1 = " << poly1 << endl;

cout << "Polynomial 1 has degree " << poly1.degree() << endl;

cout << "Polynomial 2 = " << poly2 << endl;

cout << "Polynomial 2 has degree " << poly2.degree() << endl;

//Sample test cases for operator+ and operator-

cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;

cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;

//Sample test cases for operator ==

if(poly1 == poly2)

{

cout << "Two polynomials are the same." << endl;

}

else

{

cout << "Two polynomials are different." << endl;

}

return 0;

}

//DO NOT MODIFY THIS FUNCTION

const Poly& Poly::operator= (const Poly& aPoly)

{

if(this == &aPoly){

return *this;}

if(coeff){

delete [] coeff;}

arraySize = aPoly.arraySize;

coeff = new int[arraySize];

for(int i=0; i<arraySize; ++i)

{

coeff[i] = aPoly.getCoeff(i);

}

return *this;

}

//YOUR CODE GOES HERE. COMPLETE EACH CLASS MEMBER FUNCTIONS AND FRIEND FUNCTIONS HERE:

Solutions

Expert Solution

Program

#include <cmath>

#include <iostream>

using namespace std;

const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array

//DO NOT modify the class header

class Poly
{

private:
//Data members

int arraySize; //size of array

int *coeff; //dynamic array

public:

Poly();

Poly(int size);

Poly(const Poly& aPoly);

~Poly();

const Poly& operator=(const Poly& aPolt);

void grow(int newSize);

int degree() const;

void setCoeff(int value, int i);

int getCoeff(int i) const;

void negate();

friend Poly operator+(const Poly& aPoly, const Poly& bPoly);

friend Poly operator-(const Poly& aPoly, const Poly& bPoly);

friend bool operator==(const Poly& aPoly, const Poly& bPoly);

friend ostream& operator<<(ostream& out, const Poly &aPoly);

};

//default constructor
Poly::Poly()
{
arraySize = DEFAULTPOLY;
coeff = new int[DEFAULTPOLY];
for(int i=0; i<DEFAULTPOLY; i++)
coeff[i] = 0;
}

//constructor
Poly::Poly(int size)
{
arraySize = size;
coeff = new int[size];
for(int i=0; i<size; i++)
coeff[i] = 0;

}

//copy constructor
Poly::Poly(const Poly& aPoly)
{
arraySize = aPoly.arraySize;

if(coeff){

delete [] coeff;}

coeff = new int[arraySize];

for(int i=0; i<arraySize; i++)
coeff[i] = aPoly.coeff[i];
}

//destructor
Poly::~Poly()
{
delete []coeff;
}

// = operator overloading
const Poly& Poly::operator=(const Poly& aPoly)
{
if(this == &aPoly){

return *this;}

if(coeff){

delete [] coeff;}

arraySize = aPoly.arraySize;

coeff = new int[arraySize];

for(int i=0; i<arraySize; ++i)
{
coeff[i] = aPoly.getCoeff(i);
}
return *this;
}

// function to grow the polynomial
void Poly::grow(int newSize)
{
if(arraySize>=newSize) return;

Poly temp(newSize);

for(int i=arraySize; i<newSize; i++)
temp.coeff[i] = 0;

for(int i=0; i<arraySize; i++)
temp.coeff[i] = coeff[i];

delete []coeff;

coeff = temp.coeff;
}

// function to return the degree of the polynomial
int Poly::degree() const
{
int i;
for(i=DEFAULTPOLY-1; coeff[i]==0; i--);

return i;
}

// function to set the coefficient of the polynomial
void Poly::setCoeff(int value, int i)
{
if(i>arraySize) grow(i);

coeff[i] = value;
}

// function to return coefficient of the polynomial
int Poly::getCoeff(int i) const
{
if(i>=arraySize) return 0;

return coeff[i];
}

// function to negate the polynomial
void Poly::negate()
{
for(int i=0; i<arraySize; i++)
coeff[i] = -coeff[i];

}

// + operator overloading
Poly operator+(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;

int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] + bPoly.coeff[i];

return temp;

}

// - operator overloading
Poly operator-(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;

int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] - bPoly.coeff[i];

return temp;
}

// == operator overloading
bool operator==(const Poly& aPoly, const Poly& bPoly)
{
int i;

for(i=0; i<DEFAULTPOLY; i++)
if(aPoly.coeff[i]!=bPoly.coeff[i]) return false;

return true;
}

// << operator overloading
ostream& operator<<(ostream& out, const Poly &aPoly)
{
for(int i=aPoly.degree(); i>=0; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<" ";
}
out<<endl;

return out;
}

/////////////////////////////////////////////////////////////////////////////////////////
int main() {

int numCoeff, coeffValue, coeffDegree, x;

Poly poly1, poly2;

//prompt user for number of coefficients

cout << "How many coefficients for polynomial 1?" << endl;

cin >> numCoeff;


for(int i=0; i<numCoeff; ++i)

{

cout << "Coefficient " << i+1 << " for polynomial 1:";

cin >> coeffValue >> coeffDegree;

poly1.setCoeff(coeffValue, coeffDegree);

}


cout << endl << "How many coefficients for polynomial 2?" << endl;

cin >> numCoeff;


for(int i=0; i<numCoeff; ++i)

{

cout << "Coefficient " << i+1 << " for polynomial 2:";

cin >> coeffValue >> coeffDegree;

poly2.setCoeff(coeffValue, coeffDegree);

}


//sample test cases for degree and operator <<

cout << endl << "Polynomial 1 = " << poly1 << endl;

cout << "Polynomial 1 has degree " << poly1.degree() << endl;

cout << "Polynomial 2 = " << poly2 << endl;

cout << "Polynomial 2 has degree " << poly2.degree() << endl;

//Sample test cases for operator+ and operator-

cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;

cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;

//Sample test cases for operator ==

if(poly1 == poly2)

{

cout << "Two polynomials are the same." << endl;

}

else

{

cout << "Two polynomials are different." << endl;

}

return 0;

}


Output:


Related Solutions

Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the...
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the "Game of Life". Though not a "game" in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of LIFE to show the continuing behavior of the configuration. LIFE is an organism that lives in a discrete, two-dimensional...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in which operators follow their operands; for instance, to add 3 and 4, one would write 3 4 + rather than 3 + 4 (infix notation). If there are multiple operations, operators are given immediately after their second operands; so, the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in postfix notation: 4 is first...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
Respond to the following prompt to complete the CTE assignment. Be sure to follow the instructions...
Respond to the following prompt to complete the CTE assignment. Be sure to follow the instructions in the syllabus and your grade will be determined through the use of the related rubric (also located in the syllabus). As the Baby Boom generation continues to retire from the labor force, the ratio of retirees to workers will continue to increase. How will this demographic change impact the Social Security program? What "solution" or "solutions" would best offset the harmful effects of...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and double pointers? The assignment asks to Write a program that displays a 2D multiplication table based on row and column value specified by the user. Perform a data validation to ensure the number of rows and columns given by the user exist on the interval [1, 10]. If possible, protect against inputs that contain symbols that would normally crash the program (i.e. letter, symbols,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT