CSC 202-01 Project #4

Spring 2010Program Objective:

Design and implement an ADT for single-variable polynomials (with nonnegative exponents) called Polynomial. Each

polynomial has zero or more terms, where each term includes a coefficient (a double) and an exponent (a nonnegative integer).

You can assume that exponents lie within a range from 0 to 100. The underlying data structure you should use to implement the

polynomial ADT is an array (of coefficients). The ADT should support at least the basic operations. You may work on a team (2

members at most) or work individually.Basic operations (required):

• Default constructor Polynomial() – create a null polynomial, i.e. a polynomial whose coefficients are all equal to

zero.

• Function zeroPolynomial() – reset all coefficients to zero.

• Function insert() – inserts a new coefficient into an existing polynomial. Overwrites the old coefficient value.

• Function degree()– returns the degree of a polynomial.

• Function print()– prints the polynomial on screen.Bonus items (optional):

• Write a function eval(double x) that evaluates the polynomial at the given value of x.

• Overwrite operator for addition (+) of two polynomials.

• Overwrite operator for subtraction (-) of two polynomials.

The class must be organized as two files, a header file, Polynomial.h, containing the class definition and the implementation

file, Polynomial.cpp, containing the code for the function of the class.Hints:

Interface file: the following is a suggestion of how your Polynomial.h could possibly look like.

#ifndef POLYNOMIAL_H

#define POLYNOMIAL_H

#include<iostream.h>

using namespace std;

class Polynomial

{

Public:

Polynomial( );

void zeroPolynomial( );

void print( );

int degree( );

void insert(double coeff, unsigned int exp );

//bonus functions

Polynomial operator+ (const Polynomial & rhs) const;

Polynomial operator- (const Polynomial & rhs) const;

double eval (double x);

Private:

double coeffArray[100];

int highPower;

};

#endif //POLYNOMIAL_H

Implementation file: the following is a suggestion of how your Polynomial.cpp could possibly look like.

#include<iostream.h>

#include ”Polynomial.h”

using namespace std;

Polynomial::Polynomial( )

{

//your implementation

}

void Polynomial::zeroPolynomial( )

{

//your implementation

}

void Polynomial::print( )

{

//your implementation

}

int Polynomial::degree( )

{

//your implementation

}

void Polynomial::insert(double coeff, unsigned int exp)

{

//your implementation

}

//Bonus functions

const Polynomial Polynomial::operator+ (const Polynomial & rhs) const

{

//your implementation

}

const Polynomial Polynomial::operator- (const Polynomial & rhs) const

{

//your implementation

}

double Polynomial::eval( )

{

//your implementation

}

Driver file: run the following test program, polytest.cpp to test your class. Make sure all results are correct (check your

mathematics book if necessary). Feel free to write your own test programs to ensure your solution is OK.

// Test driver for project 4 in CSC202 spring 2010.

#include <iostream.h>

#include ”Polynomial.h”

using namespace std;

int main()

{

cout<<”Drive program for the Polynomial class”<<endl;

Polynomial p;

Polynomial q;

Polynomial s;

p.insert(3.2, 3); //insert term 3.2 * x^3 into p

p.insert(-9.4, 2); //insert term -9.4 * x^2 into p

p.insert(5.0, 7); //insert term 5.0 * x^7 into p

q.inert(5.1, 0); //insert term 5.1 * x^0 into q

q.inert(-10.0, 4); //insert term -10.0 * x^4 into q

q.inert(-5.0, 7); //insert term -5.0 * x^7 into q

p.print(); //print p

q.print(); //print q

p.zeroPolynomial(); //reset p

p.print();

p.insert(-4.6, 0); //insert term -4.6 * x^0 into p

p.insert(-6.1, 8); //insert term -6.1 * x^8 into p

p.insert(5.0, 7); //insert term 5.0 * x^7 into p

p.print(); //print p

q.print(); //print q

cout<<”The degree of polynomial p is: “<<p.degree()<<endl;

cout<<”The degree of polynomial q is: “<<q.degree()<<endl;

/* bonus items

s = p + q;

s.print();

s = p - q;

s.print();

cout<<”The value of the polynomial p is: “<<p.eval(3.0)<<” when x= “<<3.0<<endl;

cout<<”The value of the polynomial q is: “<<q.eval(-3.0)<<” when x= “<<-3.0<<endl;

*/

return 0;

}

Due date and points:

Due date: 4/23/2010 (Friday). Submit a hard copy and demo in-class.

Total points: 100.

Bonus: 15 points for each bonus function.

Well, you score an 'A' for copy/paste skills, but your grade for actually asking a question is 'F'. Try again?

Ask Your Question

Weekly Poll

Did you buy anything on Amazon's Prime Day?

Discuss in The Lounge

Poll History