Question
I'm having a hard time with a data structures homework assignment. I need to adapt a test file that was made for static array implementation
I'm having a hard time with a data structures homework assignment.
I need to adapt a test file that was made for static array implementation for dynamic aray implementation, as well as fix the bugs I can't figure out within my implementation file.
The test file runs a series of tests and displays the success/failure of each. I made my way through about half of them.. But I'm stuck. The program stops running.
//File: 'poly0.h' **************************************** HEADER FILE ********************************************************* //Homework assignment Three //CS3304
#ifndef POLY0_H #define POLY0_H #include // Provides ostream #include
// If your compiler does not support namespaces, then please delete the // following line and the set of brackets that follow. using namespace std; namespace main_savitch_3 { class polynomial { public: // TYPEDEF and MEMBER CONSTANTS typedef double value_type; typedef std::size_t size_type; unsigned int degree = 3; //const static int MAX_EX = 5; // CONSTRUCTOR polynomial(); // Default constructor polynomial(double a0); // sets the X^0 coefficient only polynomial(const polynomial& source); //Copy constructor ~polynomial(); //Deconstructor
// MODIFICATION MEMBER FUNCTIONS void add_to_coef(double amount, unsigned int exponent); void assign_coef(double new_coefficient, unsigned int exponent); void clear( ); void reserve(size_t number);
void print(); // CONSTANT MEMBER FUNCTIONS double coefficient(unsigned int k) const; //Returns coefficient for indicated exponent unsigned int get_degree( ) const; unsigned int get_array_size() const; //ASSIGNMENT OPERATORS polynomial& operator = (const polynomial& source); polynomial& operator = (double a0); //EVALUATION FUNCTIONS double eval(double x) const; double operator( ) (double x) const { return eval(x); } private: value_type *coef; unsigned int array_size; //void compute_degree(); }; // NON-MEMBER BINARY OPERATORS 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: 'poly0.cpp' ******************************** IMPLEMENTATION FILE ******************************************************************************** //Author: Benjamin Casazza //Homework assignment Three //CS3304
#include "poly0.h" #include #include #include #include using namespace std; namespace main_savitch_3{ polynomial::polynomial(){ //Default Constructor coef = new double[degree]; for (unsigned int i = 0; i < degree; i++) coef[i] = 0.0; array_size = 3; degree = 0; } polynomial::polynomial(double a0){ //Constructor with coeffieicinet for x^0 coef = new double[degree]; for (unsigned int i = 0; i < degree; i++) coef[i] = 0.0; coef[0] = a0; degree = 0; array_size = 3; } polynomial::polynomial(const polynomial& source){ //Copy construtor coef = new double[source.array_size]; degree = source.degree; array_size = source.array_size; copy(source.coef, source.coef + array_size, coef); }
polynomial::~polynomial(){ // Deconstructor delete [] coef; } //MODIFICATION MEMBER FUNCTIONS void polynomial::add_to_coef(double amount, unsigned int exponent){ //increaces the coefficinet of degree exponent unsigned int expi = degree; assert(exponent < array_size); coef[exponent] += amount; if(exponent > degree && coef[exponent] != 0.0) degree = exponent; else if(exponent == degree && coef[exponent] == 0){ for(unsigned int i = 0; i < degree; i++){ if(coef[i] > 0){ expi = i; } } degree = expi; } }
void polynomial::assign_coef(double new_coefficient, unsigned int exponent){ //Sets ceofficient exponent to new-coefficient unsigned int expi = degree; assert(exponent <= array_size); coef[exponent] = new_coefficient; if(exponent > degree && coef[exponent] != 0.0){ degree = exponent; } else if(exponent == degree && coef[exponent] == 0){ for(unsigned int i = 0; i < degree; i++){ if(coef[i] > 0){ expi = i; } } degree = expi; } } void polynomial::clear(){ delete [] coef; coef = new double[3]; for (unsigned int i = 0; i < array_size; i++ ) coef[i] = 0.0; degree = 0; array_size = 3; } void polynomial::reserve(size_type number){ //Increaces degree capability //cout << "ORIGINAL ARRAY: "; //this->print(); value_type *larger_array; unsigned int original_array_size = array_size;
if (number == array_size) return; // The allocated memory is already the right size. if (number < array_size) number = array_size; // Can't allocate less than we are using.
larger_array = new value_type[number]; copy(coef, coef + array_size, larger_array); delete [ ] coef; coef = larger_array; array_size = number; for(unsigned int i = original_array_size; i < number; i++){ coef[i] = 0.0; } print(); } //CONSTANT MEMBER FUNCTIONS double polynomial::coefficient(unsigned int k) const{ //returns indicated coefficient return coef[k]; } unsigned int polynomial::get_degree() const{ //returns degree of polynomial return degree; } unsigned int polynomial::get_array_size() const{ return array_size; } //EVALUATION FUNCTIONS double polynomial::eval(double x) const{ //Exaluates a polynomial with value x double sum = 0; for(unsigned int i = 0; i < this->get_array_size(); i++) sum += coef[i] * pow(x, i); return sum; }
//OVERRIDING OPERATORS polynomial operator +(const polynomial& p1, const polynomial& p2){ polynomial p11 = p1; polynomial p22 = p2; polynomial sum; if(p11.get_array_size() > p22.get_array_size()){ sum.reserve(p11.get_array_size()); for(unsigned int u = 0; u < sum.get_array_size(); u++) { sum.assign_coef(0.0, u); //Filling sum with 0.0 } for(unsigned int i = 0; i < sum.get_array_size(); i++){ if(i < p22.get_array_size()) sum.assign_coef(p1.coefficient(i) + p2.coefficient(i), i); else sum.assign_coef(p11.coefficient(i), i); } } else if (p11.get_array_size() <= p22.get_array_size()){ sum.reserve(p22.get_array_size()); for(unsigned int u = 0; u < sum.get_array_size(); u++){ sum.assign_coef(0.0, u); //Filling sum with 0.0 } for(unsigned int i = 0; i < sum.get_array_size(); i++){ if(i < p11.get_array_size()) sum.assign_coef(p11.coefficient(i) + p22.coefficient(i), i); else sum.assign_coef(p22.coefficient(i), i); } } return sum; } polynomial operator -(const polynomial& p1, const polynomial& p2){ polynomial p11 = p1; polynomial p22 = p2; polynomial diff; if(p11.get_array_size() > p22.get_array_size()){ diff.reserve(p11.get_array_size()); for(unsigned int u = 0; u < diff.get_array_size(); u++) diff.assign_coef(0.0, u); //Filling sum with 0.0 for(unsigned int i = 0; i < diff.get_array_size(); i++){ if((p22.coefficient(i) != 0 || p11.coefficient(i) != 0) && i < p22.get_array_size()) diff.assign_coef(p1.coefficient(i) - p22.coefficient(i), i); else diff.assign_coef(p11.coefficient(i), i); } } else if (p11.get_array_size() <= p22.get_array_size()){ diff.reserve(p22.get_array_size()); for(unsigned int u = 0; u < diff.get_array_size(); u++) diff.assign_coef(0.0, u); //Filling sum with 0.0 for(unsigned int i = 0; i < diff.get_array_size(); i++){ if((p22.coefficient(i) != 0 || p11.coefficient(i) != 0) && i < p22.get_array_size()) diff.assign_coef(p1.coefficient(i) - p2.coefficient(i), i); else diff.assign_coef(p22.coefficient(i), i); } } return diff; } polynomial& polynomial::operator = (const polynomial& source){ delete [] coef; this->coef = new double[source.array_size]; this->degree = source.degree; this->array_size = source.array_size; copy(source.coef, source.coef + array_size, coef); return *this; } polynomial& polynomial::operator = (double a0){ this->clear(); coef[0] = a0; return *this; } void polynomial::print(){ unsigned int i; cout<<"DEGREEEEE: " << degree << " ARRAY_SIZE: " << array_size << endl; for(i = 0; i < array_size; i++){ cout << coef[i] << "x^" << i << " + "; } cout << endl; } }
// FILE: polyexam0.cxx ************************************************* TESTING FILE *********************************************************************
// 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.get_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 <= test.get_degree() * 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;
polynomial p;
p.reserve(5);
polynomial x_squared;
x_squared.assign_coef(1.0, 2);
polynomial also_empty;
also_empty.assign_coef(0.0, 2);
double c[8] = {0, 0, 0, 0, 0, 0, 0, 0};
double rand_c[p.get_array_size()];
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, 4);
p.add_to_coef(-p.coefficient(4), 4);
if (!correct(p, 2, c)) return 0;
cout << "Checking that assign_coef correctly adjusts degree downward." << endl;
p.assign_coef(1.0, 4);
p.assign_coef(0, 4);
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 " << p.get_array_size() << " random coefficients ";
cout << " and then checking the basic functions.";
cout << endl;
for (i = 0; i < p.get_array_size(); i++) {
rand_c[i] = ((rand() % 1000) / 100.0) + 1;
p.assign_coef(rand_c[i], i);
}
if (!correct(p, p.get_array_size()-1, 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;
p.reserve(5);
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 < p.get_array_size(); ++i) {
p.assign_coef(((rand() % 1000) / 100.0) + 1, i);
}
p.assign_coef(0.0, TESTSIZE / 2);
return POINTS[4];
}
// Test operator + function
double test5() {
polynomial p, q;
p.reserve(5);
q.reserve(5);
double c[5] = {0,4,8,12,16};
unsigned int i;
cout << "Testing operator +" << endl;
for (i = 0; i < p.get_array_size(); i++) {
p.assign_coef(i*2, i);
q.assign_coef(i*2, i);
}
if (!correct(p + q, p.get_array_size(), 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.print();
cout << 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started