Facebook
From chenqin, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 116
  1. /**
  2. * ====================================
  3. *        File Name : Sequence.h
  4. * ====================================
  5. */
  6.  
  7. #ifndef SEQUENCE_H
  8. #define SEQUENCE_H
  9. #include<cstdlib>
  10. #include<iostream>
  11. #include<cmath>
  12. using namespace std;
  13. class Sequence{
  14.    private:
  15.        double *doubleRecords;
  16.        int currentSize;
  17.        int maximumSize;
  18.    public:
  19.        Sequence() ;
  20.        Sequence(const Sequence &otherSequence);
  21.        ~Sequence();
  22.        Sequence& operator = (const Sequence &otherSequence);
  23.        int size() const;
  24.        void insert(double number);
  25.        void insert(double records[],int numberOfRecords);
  26.        int find(double number);
  27.      
  28.        double sum();
  29.        double mean();
  30.        double median();
  31.        double stddev();
  32.        Sequence concatenate(const Sequence &otherSequence);
  33.        double getElementAtIndex(int index) const;
  34.      
  35.        void reallocateWithSizeAndCopy(int size);
  36.        void sort();
  37.        void printDoubles();
  38.      
  39. };
  40. #endif
  41.  
  42. /**
  43. * ====================================
  44. *        File Name : Sequence.cpp
  45. * ====================================
  46. */
  47.  
  48. #include "Sequence.h"
  49. /**
  50. * Default constructor allocates a size of 2
  51. *    sets maximumsSize to 2
  52. * and currentSize to 0
  53. */
  54. Sequence::Sequence(){
  55.    doubleRecords = new double[2];
  56.    maximumSize = 2;
  57.    currentSize = 0;
  58. }
  59.  
  60. /**
  61. * Copy Constructor
  62. * creates a deep copy of its constant Sequence reference parameter
  63. */
  64. Sequence::Sequence(const Sequence &otherSequence){
  65.  
  66.    //allocate memory to the doubleRecords
  67.    doubleRecords = new double[otherSequence.size()];
  68.    maximumSize = otherSequence.size();
  69.    currentSize = 0;
  70.    //now copy the passed reference sequence numbers into the calling object records.
  71.    for(currentSize = 0;currentSize<maximumSize;currentSize++)
  72.    {
  73.        doubleRecords[currentSize] = otherSequence.getElementAtIndex(currentSize);
  74.    }
  75. }
  76. /**
  77. * destructor – deallocates dynamic memory associated with the object’s double (array) pointer
  78. */
  79. Sequence::~Sequence(){
  80.    //frree up the memory used by doubleRecords array
  81.    free(doubleRecords);
  82. }
  83. /**
  84. * copies its constant Sequence reference parameter to the calling object
  85. * deallocates dynamic memory associated with the calling object's original array
  86. * returns a reference to the calling object; should behave correctly during self-assignment
  87. */
  88. Sequence& Sequence::operator = (const Sequence &otherSequence){
  89.    //free up the doubleRecords memory first
  90.    free(doubleRecords);
  91.    //allocate memory of passed Sequence size.
  92.    doubleRecords = new double[otherSequence.size()];
  93.    //set maximumSize to parameter sequence size.
  94.    maximumSize = otherSequence.size();
  95.    currentSize = 0;
  96.    //copy params sequence into calling object sequence
  97.    for(currentSize = 0;currentSize<maximumSize;currentSize++)
  98.    {
  99.        doubleRecords[currentSize] = otherSequence.getElementAtIndex(currentSize);
  100.    }
  101.    return *this;
  102. }
  103. /**
  104. * inserts its double parameter at the next available index
  105. * if the underlying array is full, doubles maximum size,
  106. * creates an array of the new size, copies the contents of the
  107. * old array to the new array, frees memory associated with the old array,
  108. * and assigns new array’s address to object’s array pointer, then inserts parameter
  109. */
  110. void Sequence::insert(double number){
  111.  
  112.    //if array is full call reallocateWithSizeAndCopy() with double of currentSize.
  113.    //reallocateWithSizeAndCopy() allocates memory with new size and copies old contents
  114.    //of doubleReacords removes the memory old doubleRecords and sets the new
  115.    //records address in doubleRecords.
  116.    if(currentSize == maximumSize)
  117.    {
  118.        reallocateWithSizeAndCopy(currentSize *2);
  119.    }
  120.    //set the new number at currentSize , after that increment currentSize
  121.    doubleRecords[currentSize] = number;
  122.    currentSize++;
  123.  
  124. }
  125. void Sequence::insert(double records[],int numberOfRecords){
  126.    //if currentSize of the doubleRecords and the numberOfRecords to insert
  127.    //exceeds the maximumSize call reallocateWithSizeAndCopy() with currentSize + numberOfRecords.
  128.    //reallocateWithSizeAndCopy() allocates memory with new size and copies old contents
  129.    //of doubleReacords removes the memory old doubleRecords and sets the new
  130.    //records address in doubleRecords.
  131.    if(currentSize + numberOfRecords > maximumSize)
  132.    {
  133.        reallocateWithSizeAndCopy(currentSize + numberOfRecords);
  134.    }
  135.    //copy numbers in passed records array into doubleRecords
  136.    for(int i = 0;i<numberOfRecords;i++,currentSize++)
  137.    {
  138.        doubleRecords[currentSize] = records[i];
  139.    }
  140. }
  141. /**
  142. * takes a number and returns it's count in the calling object doubleReacords
  143. */
  144. int Sequence::find(double number){
  145.    int count = 0;
  146.    for(int i = 0;i<currentSize;i++)
  147.    {
  148.        if(doubleRecords[i] == number)
  149.        {
  150.            count++;
  151.        }
  152.    }
  153.    return count;
  154. }
  155.  
  156. /**
  157. * returns currentSize or number of records in doubleRecords array.
  158. */
  159. int Sequence::size()const
  160. {
  161.    return currentSize;
  162. }
  163.  
  164. /**
  165. * returns the sum of numbers in doubleRecords of the calling object.
  166. */
  167. double Sequence::sum()
  168. {
  169.    int sum = 0;
  170.    for(int i = 0;i<currentSize;i++)
  171.    {
  172.        sum += doubleRecords[i];
  173.    }
  174.    return sum;
  175. }
  176.  
  177. /**
  178. * returns the mean of the numbers in doubleRecors of the calling object.
  179. */
  180. double Sequence::mean()
  181. {
  182.    return sum() / currentSize;
  183. }
  184. /**
  185. * Extra method that sorts the calling object doubleRecords
  186. * this method is used at finding median().
  187. */
  188. void Sequence::sort()
  189. {
  190.    int temp;
  191.    for(int i = 0;i<currentSize;i++)
  192.    {
  193.        for(int j = 0;j<currentSize-1;j++)
  194.        {
  195.            if(doubleRecords[j] > doubleRecords[j+1])
  196.            {
  197.                temp = doubleRecords[j];
  198.                doubleRecords[j] = doubleRecords[j+1];
  199.                doubleRecords[j+1] = temp;
  200.            }
  201.        }
  202.    }
  203. }
  204.  
  205. /**
  206. * returns the median of the calling object.
  207. * to find median we have to sort the records
  208. * this may change the inserted order. so
  209. * created a temparary Sequence object using copy Constructor
  210. * since the calling object and newly created object contains same values.
  211. * we calculate the median using the temp object
  212. */
  213. double Sequence::median()
  214. {
  215.    Sequence temp(*this);
  216.    temp.sort();
  217.    int middle = currentSize/2;
  218.    //if sequence length is even
  219.    //median = (element at index (middle -1) + element at index middle) /2
  220.    //since c supports 0based index we have to consider middle-1, middle not middle and middle + 1 indexe.
  221.    if(currentSize % 2== 0){
  222.      
  223.        return (temp.getElementAtIndex(middle) + temp.getElementAtIndex(middle-1)) / 2;
  224.    }else{
  225.        //if odd return element at middle index
  226.        return doubleRecords[middle];
  227.    }
  228. }
  229. /**
  230. * returns a double equal to the standard deviation of the values in the sequence;
  231. * the standard deviation is the square root of the average of
  232. * the squared differences from the mean: sd = root(sum(difference2)/n)
  233. */
  234. double Sequence::stddev()
  235. {
  236.    double meanValue = mean();
  237.    double meanDifference;
  238.    double sumOfMeanDifference = 0;
  239.    for(int i = 0;i<currentSize;i++)
  240.    {
  241.        meanDifference = doubleRecords[i] - meanValue;
  242.      
  243.        meanDifference = pow(meanDifference,2);
  244.        sumOfMeanDifference += meanDifference;
  245.      
  246.    }
  247.  
  248.    return sqrt(sumOfMeanDifference / currentSize);
  249. }
  250. /**
  251. * returns a Sequence object that contains the contents of the calling object followed by
  252. * the contents of its constant Sequence reference parameter
  253. * the size of the returned object's underlying array should be the greater of 2 and the number of values stored in it
  254. */
  255. Sequence Sequence::concatenate(const Sequence &otherSequence)
  256. {
  257.    Sequence mergedSequence(*this);
  258.    for(int i = 0;i<otherSequence.size();i++)
  259.    {
  260.        mergedSequence.insert(otherSequence.getElementAtIndex(i));
  261.    }
  262.    return mergedSequence;
  263. }
  264. /**
  265. * Extra method that returns element at index.
  266. * to access elements in doubleRecords using other objects(passed as parameters)
  267. */
  268. double Sequence::getElementAtIndex(int index) const{
  269.    if(index < currentSize){
  270.        return doubleRecords[index];
  271.    }
  272.    return 0;  
  273. }
  274. /**
  275. * Extra method that reallocates memory with passed size
  276. * and copies the contents of doubleRecords in newly allocated array
  277. * frees the memory of doubleRecords and assignes the newly allocated array to it.
  278. */
  279. void Sequence::reallocateWithSizeAndCopy(int size){
  280.    double *newRecords = new double[size];
  281.    for(int i = 0;i<currentSize;i++)
  282.    {
  283.        newRecords[i] = doubleRecords[i];
  284.    }
  285.    free(doubleRecords);
  286.    doubleRecords = newRecords;
  287.    maximumSize = size;
  288.  
  289. }
  290. /**
  291. * Extra Method to print Doubles
  292. */
  293. void Sequence::printDoubles(){
  294.    cout<<"\n[";
  295.    for(int i = 0;i<currentSize;i++)
  296.    {
  297.        cout<< " "<<doubleRecords[i];
  298.        if(i<currentSize - 1){
  299.            cout << ",";
  300.        }
  301.    }
  302.    cout<<"]"<<endl;
  303. }
  304.  
  305. /**
  306. * ====================================
  307. *        File Name : mainc.cpp
  308. * ====================================
  309. */
  310. //if you get compile errors saying method in Sequences are not defined
  311. //replace .cpp with .h in below include statement.
  312. #include "Sequence.cpp"
  313. int main()
  314. {
  315.    Sequence one;
  316.    one.insert(1);
  317.    double numbers[] = {5,4,3,5,3,2,1};
  318.    one.insert(numbers,7);
  319.    cout<<"\n--------- Sequence One After Inserting -------"<<endl;
  320.    one.printDoubles();
  321.  
  322.    cout<<"\n========== Creating Sequence two using Copy Constructor of Sequence One -----------"<<endl;
  323.    Sequence two(one);
  324.    cout<<"\n--------- Sequence Two After Copying -------"<<endl;
  325.    two.printDoubles();
  326.  
  327.    cout<<"\n========== Creating Sequence three by assiging two to it -----------"<<endl;
  328.    Sequence three = two;
  329.    cout<<"\n--------- Sequence Three: -------"<<endl;
  330.    three.printDoubles();
  331.  
  332.  
  333.  
  334.    cout<<"\nNumber of Occurences of 5 in sequence two is : "<<two.find(5)<<endl;
  335.  
  336.  
  337.    cout<<"\nNumber of values in Sequence two is : "<<two.size()<<endl;
  338.  
  339.  
  340.    cout<<"\nSum of values in Sequence one is : "<<one.sum()<<endl;
  341.  
  342.  
  343.    cout<<"\nmean of values in Sequence three is : "<<three.mean()<<endl;
  344.  
  345.  
  346.    cout<<"\nMedian of values in Sequence one is : "<<one.median()<<endl;
  347.  
  348.  
  349.  
  350.    cout<<"\nStandard Deviation of values in Sequence one is : "<<one.stddev()<<endl;
  351.  
  352.  
  353.    cout<<"\n========== Creating Sequence four by concatenating one and two -----------"<<endl;
  354.    Sequence four = one.concatenate(two);
  355.    cout<<"\n--------- Sequence Four: -------"<<endl;
  356.    four.printDoubles();
  357.    return 0;
  358. }