/* Grant Chen Due: 10/30/08 CS213 Student ID 908306235 Compiler: Dev-C++ Exercise 6 part 2 Implementation */ #include #include "asgn6p2.h" using namespace std; VectorDouble::VectorDouble() // Default constructor { maxCount = 50; // create a dynamic array of 50 elements } VectorDouble::VectorDouble(int size) // user-defined constructor { maxCount = size; // create dynamic array of user-defined elements } VectorDouble::~VectorDouble() { delete [] elements;// delete stuff } void VectorDouble::push_back(double val) // add val to end of vector { if (count == maxCount) // If it's already at the max. { expandCapacity(); // call expandCapacity } count++; // make the filled vector one larger elements[count] = val; // and fill in that new value. } double VectorDouble::value_at(int i) const // returns element i { if (i < 0 || i > count) // if it's under 0 or above count { return 0; // terminate program } else { return elements[i]; // Otherwise, return element i } } void VectorDouble::change_value_at(double newVal, int i) { elements[i] = newVal; // places value newVal in element[i] } int VectorDouble::size() const // returns the number of elements in the array { return count; } int VectorDouble::capacity() const // returns the size of the array { return maxCount; } void VectorDouble::reserve(int size)// if size > maxCount, array length increased to size. { if (size > maxCount) { maxCount = size; } } void VectorDouble::resize(int size) { if (size > 0) { maxCount = size; // otherwise, array changed to given size. elements[maxCount] = '\0'; } // if the size is less than zero, terminate } VectorDouble::ostream& operator <<(ostream& outs, const VectorDouble& v) { for (int i = 0; i != maxCount; i++) { outs << v.elements[i]; } } void VectorDouble::expandCapacity() // teacher-written, no idea what's wrong. { maxCount = 2*maxCount; double *newElements = new double[maxCount]; if (newElements == NULL ){ cerr << "VectorDouble::expandCapacity(): Fail to allocate the required memory\n"; exit (1); } for (int i=0; i