Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Requirements The goal of assignment 3 is to reinforce the dynamic classes in C++. Specifically, the assignment is to implement the polynomial class from chapter

Requirements The goal of assignment 3 is to reinforce the dynamic classes in C++. Specifically, the assignment is to implement the polynomial class from chapter 4.6 on page 212. But the additional requirement is to implement the class using a dynamic array to store the coefficients. The class needs to have a destructor, copy constructor, and an overloaded assignment operator. The following files are provided for your reference. You should start by modifying the header file first.

// FILE: poly0.h // CLASS PROVIDED: // class polynomial (in the namespace main_savitch_3) // A polynomial has one variable x, real number coefficients, and // non-negative integer exponents. Such a polynomial can be viewed // as having the form: // A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0] // where the A[n] are the real number coefficients and x^i represents // the variable x raised to the i power. The coefficient A[0] is // called the "constant" or "zeroth" term of the polynomial. // // NOTES TO STUDENT: // 1. This version works by storing the coefficients in // a fixed array. The coefficient for the x^k term is stored // in location [k] of the fixed-size array. Later we will modify // the implementation to use a dynamic array. // 2. Note that two functions have been implemented as inline functions // in this file (the degree and operator() functions). // 3. An implementation of the make_gif function is available for // students to use at www.cs.colorado.edu/~main/projects/polygif.cxx. // // MEMBER CONSTANTS // const static size_t CAPACITY // The size of the fixed array to store the coefficients. // const static size_t MAX_EX = CAPACITY - 1; // The maximum exponent permitted. // // CONSTRUCTOR for the polynomial class // polynomial(double c = 0.0, unsigned int exponent = 0) // PRECONDITION: exponent <= MAX_EX. // POSTCONDITION: This polynomial has been create with all zero // coefficients, except for coefficient c for the specified exponent. // When used as a default constructor (using default values for // both arguments), the result is a polynomial with all zero // coefficients. // // MODIFICATION MEMBER FUNCTIONS for the polynomial class // void add_to_coef(double amount, unsigned int exponent) // PRECONDITION: exponent <= MAX_EX. // POSTCONDITION: Adds the given amount to the coefficient of the // specified exponent. // // void assign_coef(double coefficient, unsigned int exponent) // PRECONDITION: exponent <= MAX_EX. // POSTCONDITION: Sets the coefficient for the specified exponent. // // void clear( ) // POSTCONDITION: All coefficients of this polynomial are set to zero. // // CONSTANT MEMBER FUNCTIONS for the polynomial class // double coefficient(unsigned int exponent) const // POSTCONDITION: Returns coefficient at specified exponent of this // polynomial. // NOTE: for exponents > MAX_EX, the return value is always zero. // // unsigned int degree( ) const // POSTCONDITION: The function returns the value of the largest exponent // with a non-zero coefficient. // If all coefficients are zero, then the function returns zero. // // polynomial derivative( ) const // POSTCONDITION: The return value is the first derivative of this // polynomial. // // double eval(double x) const // POSTCONDITION: The return value is the value of this polynomial with the // given value for the variable x. // // unsigned int next_term(unsigned int e) const // POSTCONDITION: The return value is the next exponent n which is LARGER // than e such that coefficient(n) != 0. // If there is no such term, then the return value is zero. // // unsigned int previous_term(unsigned int e) const // POSTCONDITION: The return value is the next exponent n which is SMALLER // than e such that coefficient(n) != 0. // If there is no such term, then the return value is UINT_MAX // from . // // CONSTANT OPERATORS for the polynomial class // double operator( ) (double x) const // Same as the eval member function. // // NON-MEMBER BINARY OPERATORS for the polynomial Class // polynomial operator -(const polynomial& p1, const polynomial& p2) // POSTCONDITION: return-value is a polynomial with each coefficient // equal to the difference of the coefficients of p1 & p2 for any given // exponent. // // polynomial operator +(const polynomial& p1, const polynomial& p2) // POSTCONDITION: return-value is a polynomial with each coefficient // equal to the sum of the coefficients of p1 & p2 for any given // exponent. // // polynomial operator *(const polynomial& p1, const polynomial& p2) // PRECONDITION: p1.degree( ) + p2.degree( ) <= polynomial::MAX_EX. // POSTCONDITION: Each term of p1 has been multiplied by each term of p2, // and the answer is the sum of all these term-by-term products. // For example, if p1 is 2x^2 + 3x + 4 and p2 is 5x^2 - 1x + 7, then the // return value is 10x^4 + 13x^3 + 31x^2 + 17x + 28. // // NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class // ostream& operator << (ostream& out, const polynomial& p) // POSTCONDITION: The polynomial has been printed to ostream out, which, // in turn, has been returned to the calling function. // // void make_gif( // const polynomial& p, // const char filename[ ], // double low_x, // double high_x, // double low_y, // double high_y // ) // PRECONDITION: filename is a legal filename for a gif file. // Also (low_x < high_x) && (low_y < high_y). // POSTCONDITION: A gif file has been written to the specified filename // with a graphical representation of the polynomial in the specified // ranges (low_x...high_x and low_y...high_y). #ifndef POLY0_H #define POLY0_H #include  // Provides ostream // If your compiler does not support namespaces, then please delete the // following line and the set of brackets that follow. namespace main_savitch_3 { class polynomial { public: // CONSTANTS static const unsigned int CAPACITY = 30; static const unsigned int MAX_EX = CAPACITY - 1; // CONSTRUCTOR polynomial(double c = 0.0, unsigned int exponent = 0); // MODIFICATION MEMBER FUNCTIONS void add_to_coef(double amount, unsigned int exponent); void assign_coef(double coefficient, unsigned int exponent); void clear( ); // CONSTANT MEMBER FUNCTIONS double coefficient(unsigned int exponent) const; unsigned int degree( ) const { return current_degree; } //polynomial derivative( ) const; double eval(double x) const; unsigned int next_term(unsigned int e) const; unsigned int previous_term(unsigned int e) const; // CONSTANT OPERATORS double operator( ) (double x) const { return eval(x); } private: double coef[CAPACITY]; unsigned int current_degree; void compute_degree(); }; // NON-MEMBER BINARY OPERATORS polynomial operator +(const polynomial& p1, const polynomial& p2); polynomial operator -(const polynomial& p1, const polynomial& p2); //polynomial operator *(const polynomial& p1, const polynomial& p2); // NON-MEMBER OUTPUT FUNCTIONS std::ostream& operator << (std::ostream& out, const polynomial& p); } #endif 
// FILE: polytest0.cxx // An Interactive test program for the polynomial ADT // Written by: Kenneth R. Glover - gloverk@colorado.edu #include  // Provides toupper #include  // Provides cout and cin #include  // Provides EXIT_SUCCESS #include "poly0.h" // Provides the polynomial class using namespace std; using namespace main_savitch_3; const unsigned int MANY = 3; // Number of polynomials allowed in this test program. // PROTOTYPES for functions used by this test program: void print_menu(); // Postcondition: The menu has been written to cout. size_t set_current( ); // Postcondition: Return value is index for a new current polynomial. char get_command(); // Postcondition: The user has been prompted for a character. // The entered charatcer will be returned, translated to upper case. void view(const polynomial& test); //Postcondition: The polynomial passed has been sent to cout. void view_all(const polynomial a[]); //Postcondition: All polynomials has been written to cout. void test_add(polynomial& test); // Postcondition: The user has been prompted for a coefficent and degree of // the term added. The resulting polynomial has been written to cout. void test_assign(polynomial& test); // Postcondition: The user has been prompted for the degree and the coeffinient // to be set. The resulting polynomial has been written to cout. void test_clear(polynomial& test); // Postcondition: test.clear( ) has been activated. // to be set. The resulting polynomial has been written to cout. void test_gif(const polynomial& test); // Postcondition: The user has been prompted for the range of the gif file. // The gif file of the graph of the polynomial has been written to disk. void test_eval(const polynomial& test); // Post conditon: The user has been prompted for the x value. The evaluation // of the polynomial is written to cout. void test_np(const polynomial& test); // Post conditon: The user has been prompted for the e value. The // value of test.next_term(e) and test.previous_term(e) are written to cout. int main() { polynomial p[MANY]; size_t current_index = 0; char command; size_t i; cout << "Polynomials "; for (i = 0; i < MANY; ++i) cout << char('A' + i) << ' '; cout << "have all been initialized." << endl; do { print_menu(); command = toupper(get_command()); switch(command) { case 'S': current_index = set_current( ); break; case '1': test_assign(p[current_index]); break; case '2': test_add(p[current_index]); break; case 'C': test_clear(p[current_index]); break; case 'V': cout << char(current_index + 'A') << ": "; view(p[current_index]); break; case 'A': view_all(p); break; // case 'D': // cout << char(current_index + 'A') << ".derivative: "; // view(p[current_index].derivative( )); // break; case 'E': test_eval(p[current_index]); break; // case 'G': test_gif(p[current_index]); // break; case 'N': test_np(p[current_index]); break; case '+': cout << "A + B: "; view(p[0] + p[1]); break; case '-': cout << "A - B: "; view(p[0] - p[1]); break; // case '*': // cout << "A * B: "; // view(p[0] * p[1]); // break; case 'Q': // Do nothing.. break; default: cout << "Invalid command." << endl; break; } } while(command != 'Q'); return (EXIT_SUCCESS); } void print_menu() { cout << "----------------- The Commands -----------------" << endl; cout << "S - set the current Polynomial to work on" << endl; cout << " - - - - - - - - - - - -" << endl; cout << "1 - use the assign_coef function" << endl; cout << "2 - use the add_to_coef function" << endl; cout << "C - use the clear function" << endl; cout << "V - view the current polynomial by using <<" << endl; cout << "A - view all polynomials by using <<" << endl; cout << "D - view derivative of current polynomial" << endl; cout << "E - evaluate current polynomial by using () op" << endl; cout << "G - use the gif function" << endl; cout << "N - use the next_term and previous_term functions" << endl; cout << "+ - view A + B" << endl; cout << "- - view A - B" << endl; cout << "* - view A * B" << endl; cout << " - - - - - - - - - - - -" << endl; cout << "Q - quit this interactive test program" << endl; cout << "-------------------------------------------------" << endl; } char get_command() { char command; cout << ">"; cin >> command; return(toupper(command)); } void view(const polynomial& test) { cout << test << " (degree is " << test.degree( ) << ")" << endl; } size_t set_current( ) { size_t i; char command; do { cout << "Polynomials "; for (i = 0; i < MANY; ++i) cout << char('A' + i) << ' '; cout << "." << endl; cout << "Enter the polynomial you want to work on: "; command = toupper(get_command()); } while ((command < 'A') || (command >= char('A' + MANY))); return command - 'A'; } void test_add(polynomial& test) { double coefficient; unsigned int exponent; cout << "Enter exponent: "; cin >> exponent; cout << "Enter coefficient: "; cin >> coefficient; test.add_to_coef(coefficient, exponent); cout << "After adding: "; view(test); } void test_assign(polynomial& test) { double coefficient; unsigned int exponent; cout << "Enter exponent: "; cin >> exponent; cout << "Enter coefficient: "; cin >> coefficient; test.assign_coef(coefficient, exponent); cout << "After assigning: "; view(test); } //void test_gif(const polynomial& test) //{ // char file_name[20]; // double high_x, low_x, high_y, low_y; // // cout << "Enter file name to write: "; // cin >> file_name; // cout << "Enter upper x bound: "; // cin >> high_x; // cout << "Enter lower x bound: "; // cin >> low_x; // cout << "Enter upper y bound: "; // cin >> high_y; // cout << "Enter lower y bound: "; // cin >> low_y; // // make_gif(test, file_name, low_x, high_x, low_y, high_y); // cout << "The file has been written" << endl; //} void test_eval(const polynomial& test) { double x_value; cout << "Enter the x value: "; cin >> x_value; cout << "For the poly: "; view(test); cout << "The evaluation returned is " << test(x_value) << endl; } void view_all(const polynomial p[]) { size_t i; cout << endl; for (i = 0; i < MANY; ++i) { cout << char(i + 'A') << ": "; view(p[i]); } } void test_clear(polynomial& test) { test.clear( ); cout << "After clearing: "; view(test); } void test_np(const polynomial& test) { unsigned int exponent; cout << "Enter exponent: "; cin >> exponent; cout << "For polynomial: "; view(test); cout << "next_term(" << exponent << ") = " << test.next_term(exponent) << endl; cout << "previous_term(" << exponent << ") = " << test.previous_term(exponent) << endl; } 
// FILE: polyexam0.cxx // Written by: Michael Main (main@colorado.edu), Sep 8, 2000 // Non-interactive test program for the polynomial class // from http://www.cs.colorado.edu/~main/chapter3/poly0.cxx #include  // Provides UINT_MAX #include  // Provides fabs #include  // Provides rand #include  // Provides memcpy #include  // Provides cout #include "poly0.h" using namespace std; using namespace main_savitch_3; const unsigned int TESTSIZE = 5; // Descriptions and points for each of the tests: const size_t MANY_TESTS = 8; const int POINTS[MANY_TESTS + 1] = { //60, // Total points for all tests. 50, // we're skipping two tests here CBS 25, // Test 1 points 5, // Test 2 points 5, // Test 3 points 5, // Test 4 points 5, // Test 5 points 5, // Test 6 points 5, // Test 7 points 5 // Test 8 points }; const char DESCRIPTION[MANY_TESTS + 1][256] = { "tests for polynomial class", "Testing basic functions", "Testing derivative function", "Testing eval and operator ( )", "Testing next_term and previous_term function", "Testing operator +", "Testing operator -", "Testing operator *", "Testing operator <<" }; // ************************************************************************** // bool close(double a, double b) // Returns true if a is within EPSILON of b. // ************************************************************************** bool close(double a, double b) { const double EPSILON = 0.0001; return (fabs(a - b) < EPSILON) || (fabs(1 - a / b) < EPSILON); } // ************************************************************************** // bool correct(const polynomial& test, unsigned int d, double c[ ]) // This function determines if the polynomial (test) is "correct" according // to these requirements: // a. it's degree is d // b. For each i in the range 0...(degree), test.coefficient(i) is c[i]. // ************************************************************************** bool correct(const polynomial &test, unsigned int d, double count[]) { size_t i; bool answer = true; if (test.degree() != d) { cout << "Incorrect degree. " << endl; answer = false; } else { for (i = 0; i <= d; ++i) { if (!close(count[i], test.coefficient(i))) { cout << "Incorrect coefficient(" << i << ")" << endl; answer = false; } } for (i = d + 1; i <= polynomial::MAX_EX * 2; ++i) { if (!close(test.coefficient(i), 0)) { cout << "Incorrect-coefficient(" << i << ")" << endl; answer = false; } } cout << (answer ? "Test passed. " : "Test failed. ") << endl; } return answer; } double test1() { polynomial empty, x_squared(1.0, 2), p, also_empty(0.0, 3);; double c[8] = {0, 0, 0, 0, 0, 0, 0, 0}; double rand_c[polynomial::CAPACITY]; size_t i; cout << "I will now test the default constructor. After initializing " << "with the default constructor, a the coefficient member " << "function should always return zero (even beyond MAX_EX)." << endl; if (!correct(empty, 0, c)) return 0; cout << "I will now test the constructor with arguments." << endl; c[2] = 1.0; if (!correct(x_squared, 2, c)) return 0; cout << "I will now test constructor like this: polynomial p(0.0, 3). " << "After initializing this way, a polynomial " << "should have all zero coefficients (even beyond MAX_EX)." << endl; if (!correct(also_empty, 0, c)) return 0; cout << "I will now test assign_coef." << endl; p.assign_coef(1.0, 2); if (!correct(p, 2, c)) return 0; cout << "I will now test add_to_coef." << endl; p.add_to_coef(2.0, 2); c[2] += 2.0; if (!correct(p, 2, c)) return 0; cout << "Checking that add_to_coef correctly adjusts degree downward." << endl; p.assign_coef(1.0, 10); p.add_to_coef(-p.coefficient(10), 10); if (!correct(p, 2, c)) return 0; cout << "Checking that assign_coef correctly adjusts degree downward." << endl; p.assign_coef(1.0, 10); p.assign_coef(0, 10); if (!correct(p, 2, c)) return 0; cout << "I will now test the clear function." << endl; p.clear(); c[2] = 0.0; if (!correct(p, 0, c)) return 0; cout << "Inserting " << polynomial::CAPACITY << " random coefficients "; cout << " and then checking the basic functions."; cout << endl; for (i = 0; i < polynomial::CAPACITY; i++) { rand_c[i] = ((rand() % 1000) / 100.0) + 1; p.assign_coef(rand_c[i], i); } if (!correct(p, polynomial::MAX_EX, rand_c)) return 0; return POINTS[1]; } // Test derivative function //double test2( ) //{ // polynomial p; // double c[TESTSIZE]; // unsigned int i; // // cout << "Testing derivative." << endl; // p.assign_coef(((rand( ) % 1000)/100.0), 0); // for (i = 1; i <= TESTSIZE; ++i) // { // p.assign_coef(((rand( ) % 1000)/100.0), i); // c[i-1] = i * p.coefficient(i); // } // p.assign_coef(0.0, TESTSIZE/2); // c[(TESTSIZE/2)-1] = 0.0; // if (!correct(p.derivative( ), TESTSIZE-1, c)) return 0; // // return POINTS[2]; //} // Testing eval double test3() { polynomial p; double value = 0.0; double power_of_x = 1.0; unsigned int i; const double X = 3.0; double c; cout << "Testing eval and operator ( )." << endl; for (i = 0; i < TESTSIZE; ++i) { c = ((rand() % 1000) / 100.0); if (i == TESTSIZE / 2) c = 0.0; p.assign_coef(c, i); value += c * power_of_x; power_of_x *= X; } if (!close(p.eval(X), value)) return 0; if (!close(p(X), value)) return 0; return POINTS[3]; } // Test next_term function double test4() { polynomial p; unsigned int i; for (i = 0; i <= TESTSIZE; ++i) { p.assign_coef(((rand() % 1000) / 100.0) + 1, i); } p.assign_coef(0.0, TESTSIZE / 2); cout << "Testing next_term function." << endl; for (i = 0; i < TESTSIZE; ++i) { if ((i != (TESTSIZE / 2) - 1) && (p.next_term(i) != i + 1)) return 0; } if (p.next_term((TESTSIZE / 2) - 1) != (TESTSIZE / 2) + 1) return 0; for (i = TESTSIZE; i < 2 * polynomial::MAX_EX; ++i) { if (p.next_term(i) != 0) return 0; } cout << "Next Term Function is okay." << endl; cout << "Testing previous_term function." << endl; for (i = 1; i <= TESTSIZE; ++i) { if ((i != (TESTSIZE / 2) + 1) && (p.previous_term(i) != i - 1)) return 0; } cout << "First for" << endl; if (p.previous_term((TESTSIZE / 2) + 1) != (TESTSIZE / 2) - 1) return 0; cout << "0 term skipped" << endl; for (i = TESTSIZE + 1; i < 2 * polynomial::MAX_EX; ++i) { if (p.previous_term(i) != TESTSIZE) return 0; } cout << "Second for" << endl; p.assign_coef(0, 0); if (p.previous_term(1) != UINT_MAX) return 0; cout << "run off bottom" << endl; cout << "Testing previous_term(0)" << endl; if (p.previous_term(0) != UINT_MAX) return 0; return POINTS[4]; } // Test operator + function double test5() { polynomial p, q; double c[TESTSIZE + 1]; unsigned int i; cout << "Testing operator +" << endl; for (i = 0; i < TESTSIZE; ++i) { p.assign_coef(((rand() % 1000) / 100.0), i); q.assign_coef(((rand() % 1000) / 100.0), i); c[i] = p.coefficient(i) + q.coefficient(i); } p.assign_coef(((rand() % 1000) / 100.0) + 1, TESTSIZE); c[5] = p.coefficient(TESTSIZE); if (!correct(p + q, TESTSIZE, c)) return 0; return POINTS[5]; } // Test operator - function double test6() { polynomial p, q; double c[TESTSIZE + 1]; unsigned int i; cout << "Testing operator -" << endl; for (i = 0; i < TESTSIZE; ++i) { p.assign_coef(((rand() % 1000) / 100.0), i); q.assign_coef(((rand() % 1000) / 100.0), i); c[i] = p.coefficient(i) - q.coefficient(i); } p.assign_coef(((rand() % 1000) / 100.0) + 1, TESTSIZE); c[5] = p.coefficient(TESTSIZE); if (!correct(p - q, TESTSIZE, c)) return 0; return POINTS[6]; } // Test operator * function //double test7( ) //{ // polynomial p, q; // double c[2*TESTSIZE + 1]; // unsigned int i,j; // // cout << "Testing operator *" << endl; // for (i = 0; i < TESTSIZE; ++i) // { // p.assign_coef(((rand( ) % 1000)/100.0), i); // q.assign_coef(((rand( ) % 1000)/100.0), i); // } // p.assign_coef(((rand( ) % 1000)/100.0)+1, TESTSIZE); // q.assign_coef(((rand( ) % 1000)/100.0)+1, TESTSIZE); // // for (i = 0; i <= 2 * TESTSIZE; ++i) // c[i] = 0.0; // // for (i = 0; i <= TESTSIZE; ++i) // for (j = 0; j <= TESTSIZE; ++j) // c[i+j] += p.coefficient(i) * q.coefficient(j); // // if (!correct(p*q, 2*TESTSIZE, c)) return 0; // // return POINTS[7]; //} // Write a polynomial to cout for visual inspection double test8() { polynomial p; p.assign_coef(-5.5, 5); p.assign_coef(+3.3, 3); p.assign_coef(-2.2, 2); p.assign_coef(1.1, 1); p.assign_coef(10.0, 0); cout << "Printing a polynomial for visual inspection by the TA: " << p << endl; return POINTS[8]; } double run_a_test(int number, const char message[], double test_function(), int max) { double result; cout << endl << "START OF TEST " << number << ":" << endl; cout << message << " (" << max << " points)." << endl; result = test_function(); if (result > 0) { cout << "Test " << number << " got " << result << " points"; cout << " out of a possible " << max << "." << endl; } else cout << "Test " << number << " failed." << endl; cout << "END OF TEST " << number << "." << endl << endl; return result; } // ************************************************************************** // int main( ) // The main program calls all tests and prints the sum of all points // earned from the tests. // ************************************************************************** int main() { double sum = 0; cout << "Running " << DESCRIPTION[0] << endl; sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); // sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); // sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]); sum += run_a_test(8, DESCRIPTION[8], test8, POINTS[8]); cout << "If you submit this to HAL now, you will have "; cout << sum << " points out of the "; cout << POINTS[0]; cout << " points from this test program. "; return EXIT_SUCCESS; } 

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

More Books

Students also viewed these Databases questions

Question

=+25-5 Identify the hallucinogens, and describe their effects.

Answered: 1 week ago