#include "vector.h" #include using namespace std; //Definitions VectorDouble::VectorDouble() { DoublePtr = new double[1];//allocates an array for 1 double count = 0; max_count = 1; } VectorDouble::VectorDouble(int capacity) { DoublePtr = new double[capacity]; for( int i = 0; i < capacity; i++) { DoublePtr[i] = 0; } count = 0; max_count = capacity; } VectorDouble::~VectorDouble()//destructor { delete [] DoublePtr; } void VectorDouble::push_back( double element ) { if ( (count) == (max_count) ) { double *SubPtr; SubPtr = new double[max_count*2];//new operator= new dynamic variable of subptr and returns a pointer to a new variable for( int i = 0; i < count; i++) { SubPtr[i]=DoublePtr[i]; } max_count *= 2;//Multiply and assign the number 2 delete [] DoublePtr; DoublePtr = new double[max_count];//new operator= new dynamic variable of subptr and returns a pointer to a new variable DoublePtr = SubPtr; DoublePtr[count] = element; count++; } /* else if ( NewPtr > count) { cout << "NULL" << endl; } else if ( NewPtr > max_count) { cout << "Cannot read beyond vector size!" << endl; }*/ else { DoublePtr[count] = element; count++; } } int VectorDouble::capacity() { return ( max_count );//retrieves max_count variable } int VectorDouble::size() { return ( count );//retrieves size variable } double VectorDouble::value_at(int postion_xx) // postion of dynamic array { if ( (postion_xx > max_count) && (postion_xx < 0) ) { cout << "Not Possible for this program!" << endl; } else if ( postion_xx > count) { cout << "NULL" << endl; } else if ( postion_xx > max_count) { cout << "Cannot read beyond vector size!" << endl; }/**/ else { return ( DoublePtr[postion_xx] ); } } void VectorDouble::change_value_at(int postion_xx, double double_xx)//changes the double value at ith element of the dynamic array to d { if(postion_xx >= 0 && postion_xx < max_count) { DoublePtr[postion_xx] = double_xx; } else { cout << "Not Possible for this program!" << endl; } }