Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have this c++ project and I cannot for the life of me get it to compile. Can someone correct this code please? It's

Hello,

I have this c++ project and I cannot for the life of me get it to compile. Can someone correct this code please? It's urgent.

polynomial.h

#ifndef POLY_H #define POLY_H #include #include

using namespace std;

const unsigned int DEFAULT_CAPACITY = 5; typedef size_t size_type; typedef double value_type;

class polyno { public: // CONSTRUCTORS and DESTRUCTOR polyno(); polyno(const polyno& source); ~polyno( );

// MODIFICATION MEMBER FUNCTIONS void operator =(const polyno& source); void assign_coef(double coefficient, unsigned int exponent); void reserve(unsigned int number); void setcurrent_degree(int init_degree); void setsize(int init_size);

// CONSTANT MEMBER FUNCTIONS double coefficient(unsigned int exponent) const; unsigned int degree( ) const; double eval(double x) const; unsigned int getsize ( ) const; private: double coef[]; double *coef_array = coef[]; // Pointer to the dynamic array unsigned int size; // Current size of the dynamic array unsigned int current_degree; // Current degree of the polyno }; // NON-MEMBER FUNCTIONS ostream& operator << (ostream& cout, const polyno& r); polyno operator +(const polyno& l, const polyno& r); polyno operator -(const polyno& l, const polyno& r);

#endif

polynomial.cpp

#include // Provides assert #include // Provides pow #include #include #include "polynomial.h"

using namespace std; //contructors n destructors polyno::polyno() { current_degree = 0; for ( int i = 0; i <= DEFAULT_CAPACITY; i++ ) coef[i];

}

polyno::polyno(const polyno& source) { coef[100] = new double[source.size]; size = source.size; current_degree = source.current_degree; for (int i = 0; i < size; i++) coef[i] = source.coef[i]; }

polyno::~polyno( ) { delete [] coef; }

//mod member fns void polyno::assign_coef(double coefficient, unsigned int exponent) {

int i = current_degree; while ( i >= 0 ) { current_degree--; i--; } } void polyno::operator =(const polyno& source) { double *new_coef; if (this == &source) return; if (size != source.size) { new_coef = new double[source.size]; delete [ ] coef; *coef = new_coef; size = source.size; } current_degree = source.current_degree; for(size_t i = 0; i < size; i++) coef[i] = source.coef[i]; }

void polyno::reserve(unsigned int number) { double *larger_array; if (number == size) return; if (number < current_degree) number = current_degree;

larger_array = new double[number](); for (int i = 0; i < number; i++) { larger_array[i] = *coef[i]; } delete [ ] coef; *coef = larger_array; size = number; } void polyno::setcurrent_degree(int init_degree) { init_degree = current_degree; }

void polyno::setsize(int init_size) { init_size = size; } // CONSTANT MEMBER FUNCTIONS double polyno::coefficient(unsigned int exponent) const { double coefficient; *coef[exponent] = coefficient; return coefficient; } unsigned int polyno::degree( ) const { { int d = 0; for ( int i = 0; i < 10; i++ ) if ( coef[i] != 0 ) d = i; return d; } } double polyno::eval(double x) const { double power = 1, result = 0;

for (unsigned int i = 0; i <= current_degree; i++) { result += *coef[i] * power; power *= current_degree; } return result;

}

unsigned int polyno::getsize ( ) const { return size; }

//nonmember fns

ostream & operator<<(ostream & out, const polyno & r) { if ( r.degree( ) == -1 ) { cout << 0; return out; } out << r.coefficient( r.degree( ) ) << "x^" << r.degree( ); for (int i = r.degree( )-1; i >= 0; i--) { out << " + "; if ( r.coefficient( i ) < 0.0 ) out << '(' << r.coefficient( i ) << ')'; else out << r.coefficient( i ); out << "x^" << i; }

return out; } polyno operator+(const polyno & l, const polyno & r ) { int leftDegree = l.degree( ), rightDegree = r.degree( ), degree; double sum; if ( leftDegree > rightDegree ) degree = leftDegree; else degree = rightDegree; polyno resPoly;

for ( int i=degree; i >= 0; i-- ) { sum = l.coefficient( i ) + r.coefficient( i ); }

return resPoly; }

poly_class.cpp

#include #include "polynomial.h" #include "polynomial.cpp"

using namespace std;

void cctest(polyno);

int main() { polyno S1, S2, S3; int howmany; double coefficient, x, result; unsigned int exponent; cout << "Enter how many coeffiecients the polyno : "; cin >> howmany; while (howmany > 0) { cout << "Enter coefficient : "; cin >> coefficient; cout << "Enter exponent : "; cin >> exponent; S1.assign_coef(coefficient, exponent); howmany--; } cout << "Enter how many coeffiecients the polyno : "; cin >> howmany; while (howmany > 0) { cout << "Enter coefficient : "; cin >> coefficient; cout << "Enter exponent : "; cin >> exponent; S2.assign_coef(coefficient, exponent); howmany--; } cout << "The first polyno is " << S1 << endl << "The second polyno is " << S2 << endl << endl; S3 = S2; cout << "Testing = operator ... " << endl << "The second polyno is " << S3 << endl << endl;

cout << "Results of Addition : " << S1 + S2 << endl; // cout << "Results of Subtraction : " << S1 - S2 << endl << endl; // cout << "Results of Multiplication : " << S1 * S2 << endl << endl; cout << "Testing Evaluate ..." << endl; cout << " Enter value of x : "; cin >> x; result = S1.eval(x); cout << " The result of S1 when x is " << x << " is " << result << endl << endl; cout << "Testing copy constructor..." << endl; cctest(S1); return 0; }

void cctest(polyno P) { cout << " The first polyno is " << P << endl << endl; }

below are the requirements.

CS 202 - Project 2 -- Polynomial Class PROJECT INSTRUCTIONS You are to implement a polynomial class and driver program for testing it. The code you produce must compile with GNU g++ on the Linux machine. Links Here are links to show you how (or remind you how) to add, subtract, multiply, and do calculus on polynomials: http://www.purplemath.com/modules/polyadd.htm http://www.purplemath.com/modules/polyadd2.htm http://www.purplemath.com/modules/polymult.htm https://www.freemathhelp.com/derivative-of-polynomial.html http://hyperphysics.phy-astr.gsu.edu/hbase/intpol.html Consult these web pages as references, if you need, when you write the member functions for your class. Discussion Example polynomial 1: 3x2 + 5x 17 Usesthe integer coefficients: 3, 5, -17 Example polynomial 2: 3x5 + 5x 17 Usesthe integer coefficients: 3, 0, 0, 0, 5, -17 Notice that zeros are used for powers of x that do not show when we write the polynomial by hand. Each example polynomial can be defined by a set of coefficients: { 3, 5, -17 } and { 3, 0, 0, 0, 5, -17 } The "degree" of a polynomial is the largest power of x. For example polynomial 1, the degree is 2. For example polynomial 2, the degree is 5. Notice that any constant (e.g., -17) is a polynomial whose degree is zero. To make this project doable, our polynomials shall have only integer coefficients and shall be of degree 5 or less. PROGRAM REQUIREMENTS You shall write your class as two files, a header file, Polynomial.h, that will contain the class definition. And an implementation file, Polynomial.cpp, that will contain constructor, destructor, and member functions. Unless a member function is declared "inline", its body shall reside in the implementation file. The driver (containing main()) shall reside in a third source code file, named poly_class.cpp. Functionally, your polynomial class shall implement: 1) object construction, using the default constructor 2) object construction, using an overloaded constructor 3) object construction, using a copy constructor 4) object destruction 5) object assignment, overloading the = operator 6) object output, overloading the << operator 7) object input, overloading the >> operator 8) object addition, overloading the + operator 9) object subtraction, overloading the - operator 10) object multiplication, overloading the * operator 11) object equality testing, overloading the == operator 12) object evaluation, overloading the () operator, (value of polynomial for a given x, e.g. f (x)). Optionally, you may choose to implement member functions for: 13) differentiation, overloading the -- operator 14) indefinite integration, overloading the ++ operator 15) definite integration, overloading the ++ operator, (over an interval) If you need more of a challenge, generalize the class so it will accept and work with polynomials of any degree. 16) arbitrary polynomials -2- When you are finished developing the class and its driver, test it. Document your tests by taking screenshots. Use Alt+PrtScn to copy the active window, and paste it into an MS-Word document. When making your Word doc, include a sentence for each screenshot that tells what is going on in it: what the test was, what the result was, etc. Your Word doc should include screenshots for testing every function you implement, along with description. Your MS-Word document should be named: screenshots.docx. Restrictions No global variables shall be used. CODING CONVENTION Each file shall have a file header. Each function shall have a function header. Your code shall be properly indented and commented. GENERAL PROJECT REQUIREMENTS Programs submitted as part of the project must have an executable built on the Linux server. Programs submitted as part of the project must satisfy the requirements of the project. The student must present the project to the class, discussing the projects requirements and how the program satisfies them, giving sample runs for the program, walking through the source codes for the program, discussing the projects makefile, discussing the programs shortcomings and known bugs. ZERO CREDIT FOR A PROJECT In general, several things can cause a student to receive zero (0) credit for a submitted assignment. 1. submitting a project late ("late" means after the due date/time) 2. submitting code they did not write 3. submitting a program that does not run or crashes 4. submitting a program that runs, but does not satisfy the projects requirements SUBMISSION REQUIREMENTS Submit a "tgz" file named Proj2.tgz that contains the entire project: executable, source files, and makefile. Make sure all your files are in your: Proj2 directory. cd .. to move up one level in your directory structure. tar -czvf Proj2.tgz Proj2 to create a Linux archive ZIP file. tar -tvf Proj2.tgz to see an index of what is in the "tgz" file. Use WinSCP to copy your ".tgz" file from the Linux server over to your Windows machine. Upload the two files to Canvas: Proj2.tgz and screenshots.docx. Submission verification is possible because you are allowed to download files from Canvas. You should test your submission by performing the following procedure: 1. Download your "tgz" file from the Canvas drop box. 2. Unzip your "tgz" file (extract its contents). 3. Transfer the file(s) to the server. 4. Be sure to do this in a different "test" directory, so you dont overwrite previous work. 5. Run the resulting program. If steps 1-5 do not work for you, steps 1-5 will not work for your instructor either. If steps 1-5 do not work for you, fix your project until steps 1-5 work for you. Only when steps 1-5 work for you can you consider your submission complete.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions