/** * ==================================== * File Name : Sequence.h * ==================================== */ #ifndef SEQUENCE_H #define SEQUENCE_H #include #include #include using namespace std; class Sequence{ private: double *doubleRecords; int currentSize; int maximumSize; public: Sequence() ; Sequence(const Sequence &otherSequence); ~Sequence(); Sequence& operator = (const Sequence &otherSequence); int size() const; void insert(double number); void insert(double records[],int numberOfRecords); int find(double number); double sum(); double mean(); double median(); double stddev(); Sequence concatenate(const Sequence &otherSequence); double getElementAtIndex(int index) const; void reallocateWithSizeAndCopy(int size); void sort(); void printDoubles(); }; #endif /** * ==================================== * File Name : Sequence.cpp * ==================================== */ #include "Sequence.h" /** * Default constructor allocates a size of 2 * sets maximumsSize to 2 * and currentSize to 0 */ Sequence::Sequence(){ doubleRecords = new double[2]; maximumSize = 2; currentSize = 0; } /** * Copy Constructor * creates a deep copy of its constant Sequence reference parameter */ Sequence::Sequence(const Sequence &otherSequence){ //allocate memory to the doubleRecords doubleRecords = new double[otherSequence.size()]; maximumSize = otherSequence.size(); currentSize = 0; //now copy the passed reference sequence numbers into the calling object records. for(currentSize = 0;currentSize maximumSize) { reallocateWithSizeAndCopy(currentSize + numberOfRecords); } //copy numbers in passed records array into doubleRecords for(int i = 0;i doubleRecords[j+1]) { temp = doubleRecords[j]; doubleRecords[j] = doubleRecords[j+1]; doubleRecords[j+1] = temp; } } } } /** * returns the median of the calling object. * to find median we have to sort the records * this may change the inserted order. so * created a temparary Sequence object using copy Constructor * since the calling object and newly created object contains same values. * we calculate the median using the temp object */ double Sequence::median() { Sequence temp(*this); temp.sort(); int middle = currentSize/2; //if sequence length is even //median = (element at index (middle -1) + element at index middle) /2 //since c supports 0based index we have to consider middle-1, middle not middle and middle + 1 indexe. if(currentSize % 2== 0){ return (temp.getElementAtIndex(middle) + temp.getElementAtIndex(middle-1)) / 2; }else{ //if odd return element at middle index return doubleRecords[middle]; } } /** * returns a double equal to the standard deviation of the values in the sequence; * the standard deviation is the square root of the average of * the squared differences from the mean: sd = root(sum(difference2)/n) */ double Sequence::stddev() { double meanValue = mean(); double meanDifference; double sumOfMeanDifference = 0; for(int i = 0;i