- /**
- * ====================================
- * File Name : Sequence.h
- * ====================================
- */
- #ifndef SEQUENCE_H
- #define SEQUENCE_H
- #include<cstdlib>
- #include<iostream>
- #include<cmath>
- 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;currentSize++)
- {
- doubleRecords[currentSize] = otherSequence.getElementAtIndex(currentSize);
- }
- }
- /**
- * destructor – deallocates dynamic memory associated with the object’s double (array) pointer
- */
- Sequence::~Sequence(){
- //frree up the memory used by doubleRecords array
- free(doubleRecords);
- }
- /**
- * copies its constant Sequence reference parameter to the calling object
- * deallocates dynamic memory associated with the calling object's original array
- * returns a reference to the calling object; should behave correctly during self-assignment
- */
- Sequence& Sequence::operator = (const Sequence &otherSequence){
- //free up the doubleRecords memory first
- free(doubleRecords);
- //allocate memory of passed Sequence size.
- doubleRecords = new double[otherSequence.size()];
- //set maximumSize to parameter sequence size.
- maximumSize = otherSequence.size();
- currentSize = 0;
- //copy params sequence into calling object sequence
- for(currentSize = 0;currentSize<maximumSize;currentSize++)
- {
- doubleRecords[currentSize] = otherSequence.getElementAtIndex(currentSize);
- }
- return *this;
- }
- /**
- * inserts its double parameter at the next available index
- * if the underlying array is full, doubles maximum size,
- * creates an array of the new size, copies the contents of the
- * old array to the new array, frees memory associated with the old array,
- * and assigns new array’s address to object’s array pointer, then inserts parameter
- */
- void Sequence::insert(double number){
- //if array is full call reallocateWithSizeAndCopy() with double of currentSize.
- //reallocateWithSizeAndCopy() allocates memory with new size and copies old contents
- //of doubleReacords removes the memory old doubleRecords and sets the new
- //records address in doubleRecords.
- if(currentSize == maximumSize)
- {
- reallocateWithSizeAndCopy(currentSize *2);
- }
- //set the new number at currentSize , after that increment currentSize
- doubleRecords[currentSize] = number;
- currentSize++;
- }
- void Sequence::insert(double records[],int numberOfRecords){
- //if currentSize of the doubleRecords and the numberOfRecords to insert
- //exceeds the maximumSize call reallocateWithSizeAndCopy() with currentSize + numberOfRecords.
- //reallocateWithSizeAndCopy() allocates memory with new size and copies old contents
- //of doubleReacords removes the memory old doubleRecords and sets the new
- //records address in doubleRecords.
- if(currentSize + numberOfRecords > maximumSize)
- {
- reallocateWithSizeAndCopy(currentSize + numberOfRecords);
- }
- //copy numbers in passed records array into doubleRecords
- for(int i = 0;i<numberOfRecords;i++,currentSize++)
- {
- doubleRecords[currentSize] = records[i];
- }
- }
- /**
- * takes a number and returns it's count in the calling object doubleReacords
- */
- int Sequence::find(double number){
- int count = 0;
- for(int i = 0;i<currentSize;i++)
- {
- if(doubleRecords[i] == number)
- {
- count++;
- }
- }
- return count;
- }
- /**
- * returns currentSize or number of records in doubleRecords array.
- */
- int Sequence::size()const
- {
- return currentSize;
- }
- /**
- * returns the sum of numbers in doubleRecords of the calling object.
- */
- double Sequence::sum()
- {
- int sum = 0;
- for(int i = 0;i<currentSize;i++)
- {
- sum += doubleRecords[i];
- }
- return sum;
- }
- /**
- * returns the mean of the numbers in doubleRecors of the calling object.
- */
- double Sequence::mean()
- {
- return sum() / currentSize;
- }
- /**
- * Extra method that sorts the calling object doubleRecords
- * this method is used at finding median().
- */
- void Sequence::sort()
- {
- int temp;
- for(int i = 0;i<currentSize;i++)
- {
- for(int j = 0;j<currentSize-1;j++)
- {
- if(doubleRecords[j] > 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<currentSize;i++)
- {
- meanDifference = doubleRecords[i] - meanValue;
- meanDifference = pow(meanDifference,2);
- sumOfMeanDifference += meanDifference;
- }
- return sqrt(sumOfMeanDifference / currentSize);
- }
- /**
- * returns a Sequence object that contains the contents of the calling object followed by
- * the contents of its constant Sequence reference parameter
- * the size of the returned object's underlying array should be the greater of 2 and the number of values stored in it
- */
- Sequence Sequence::concatenate(const Sequence &otherSequence)
- {
- Sequence mergedSequence(*this);
- for(int i = 0;i<otherSequence.size();i++)
- {
- mergedSequence.insert(otherSequence.getElementAtIndex(i));
- }
- return mergedSequence;
- }
- /**
- * Extra method that returns element at index.
- * to access elements in doubleRecords using other objects(passed as parameters)
- */
- double Sequence::getElementAtIndex(int index) const{
- if(index < currentSize){
- return doubleRecords[index];
- }
- return 0;
- }
- /**
- * Extra method that reallocates memory with passed size
- * and copies the contents of doubleRecords in newly allocated array
- * frees the memory of doubleRecords and assignes the newly allocated array to it.
- */
- void Sequence::reallocateWithSizeAndCopy(int size){
- double *newRecords = new double[size];
- for(int i = 0;i<currentSize;i++)
- {
- newRecords[i] = doubleRecords[i];
- }
- free(doubleRecords);
- doubleRecords = newRecords;
- maximumSize = size;
- }
- /**
- * Extra Method to print Doubles
- */
- void Sequence::printDoubles(){
- cout<<"\n[";
- for(int i = 0;i<currentSize;i++)
- {
- cout<< " "<<doubleRecords[i];
- if(i<currentSize - 1){
- cout << ",";
- }
- }
- cout<<"]"<<endl;
- }
- /**
- * ====================================
- * File Name : mainc.cpp
- * ====================================
- */
- //if you get compile errors saying method in Sequences are not defined
- //replace .cpp with .h in below include statement.
- #include "Sequence.cpp"
- int main()
- {
- Sequence one;
- one.insert(1);
- double numbers[] = {5,4,3,5,3,2,1};
- one.insert(numbers,7);
- cout<<"\n--------- Sequence One After Inserting -------"<<endl;
- one.printDoubles();
- cout<<"\n========== Creating Sequence two using Copy Constructor of Sequence One -----------"<<endl;
- Sequence two(one);
- cout<<"\n--------- Sequence Two After Copying -------"<<endl;
- two.printDoubles();
- cout<<"\n========== Creating Sequence three by assiging two to it -----------"<<endl;
- Sequence three = two;
- cout<<"\n--------- Sequence Three: -------"<<endl;
- three.printDoubles();
- cout<<"\nNumber of Occurences of 5 in sequence two is : "<<two.find(5)<<endl;
- cout<<"\nNumber of values in Sequence two is : "<<two.size()<<endl;
- cout<<"\nSum of values in Sequence one is : "<<one.sum()<<endl;
- cout<<"\nmean of values in Sequence three is : "<<three.mean()<<endl;
- cout<<"\nMedian of values in Sequence one is : "<<one.median()<<endl;
- cout<<"\nStandard Deviation of values in Sequence one is : "<<one.stddev()<<endl;
- cout<<"\n========== Creating Sequence four by concatenating one and two -----------"<<endl;
- Sequence four = one.concatenate(two);
- cout<<"\n--------- Sequence Four: -------"<<endl;
- four.printDoubles();
- return 0;
- }