Thread: costum grow() not working

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    costum grow() not working

    The following is the header file for Deque.cpp:

    Code:
    #ifndef DEQUE_H
    #define DEQUE_H
    
    #include <iostream>
    
    using namespace std;
    
    typedef int Item_type;
    
    class Deque
    {
    public:
       Deque();
       Deque( const Deque& other );
       ~Deque();
       void add( const Item_type& item, bool leftEnd = false );
       Item_type remove( bool leftEnd = true );
       bool empty() const;
       int size() const;
       Deque& operator=( const Deque& other );
       void  print( bool verbose = false ) const;
    
    private:
       static const int CAPACITY = 10;     // Initial number of items allowed
       static const float GROWTH;          // Growth factor to expand data
    
       int maxCount;             // Size of the array
       int count;                // Number of values currently in the Deque
       int front;                // First used entry in the array
       Item_type* data;          // Dynamic array of values in the Deque
    
       void copy( const Deque& other );
       void clear();
       void grow();
    };
    
    Deque operator+( const Deque& left, const Deque& right );
    bool operator==( const Deque& left, const Deque& right );
    
    #endif
    And this is a piece of Deque.cpp (definition file):

    Code:
    #include <iostream>
    #include <iomanip>
    #include "Deque.h"
    
    using namespace std;
    
    const float Deque::GROWTH = 1.5;
    
    Deque::Deque()
    // A new Deque is created with no items in it.
    {
       count = 0;
       front = 0;
       maxCount = CAPACITY;
       data = new Item_type[maxCount];
    }
    
    ... some function definitions ...
    
    void Deque::add( const Item_type& item, bool leftEnd )
    {
       if (count>=maxCount) grow();
       ... some code here ...
    }
    
    ... some function definitions ...
    
    void Deque::grow()
    // Grow helper function
    // The array is enlarged with it entries copied to the new array
    {
        Item_type* temp;
    	int old_maxCount = maxCount;
        maxCount = maxCount + int(GROWTH*maxCount);
        temp = new Item_type[ maxCount ];
    
        for ( int i = front; i < ((front+count)%old_maxCount); i++ ){
            temp[ i ] = data[ i ];
        }
        delete [] data;
        data = temp;
    }
    So as you may guess when add() is invoked it checks to see if the Deque needs to be enlarged. If so then executes grow(), then adds the entry and counts up (which are not included in the above).
    The problem is that grow() doesn't work properly. This is what it outs when the array has a size of 40:
    Code:
     ============================================
     Deque has 40 entries.
     [ 0]=  0 [ 1]=  0 [ 2]=  0 [ 3]=  0 [ 4]=  0
     [ 5]=  0 [ 6]=  0 [ 7]=  0 [ 8]=  0 [ 9]=  0
     [10]=  0 [11]=  0 [12]=  0 [13]=  0 [14]=  0
     [15]=  0 [16]=  0 [17]=  0 [18]=  0 [19]=  0
     [20]=  0 [21]=  0 [22]=  0 [23]=  0 [24]=  0
     [25]=126 [26]=127 [27]=128 [28]=129 [29]=130
     [30]=131 [31]=132 [32]=133 [33]=134 [34]=135
     [35]=136 [36]=137 [37]=138 [38]=139 [39]=140
     ============================================
    The instruction that created the above is:
    Code:
    Deque A;
    for (int i=0; i<40; i++) A.add( 101+i );
    All advises are greatly appreciated.
    Last edited by Opel_Corsa; 02-17-2006 at 07:27 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    i < ((front+count)%old_maxCount)

    maybe I'm missing something here, but shouldn't that be:

    i < (front+count)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    costum Deque misbehaving

    Thank you! That was it... I am now having trouble with the following:

    Code:
    void Deque::add( const Item_type& item, bool leftEnd )
    // Add an item to the rear or front of deque
    // The item is added to the Deque at the rear (or front if
    // the optional parameter is true)
    {
       if (count>=maxCount) grow();
       if (leftEnd){
    	   data[(front-1)%maxCount]=item;
    	   --front;
       }
       else{ 
    	   data[(count+front)%maxCount]=item;}
       count++;
    }
    
    Item_type Deque::remove( bool leftEnd )
    // Remove item from rear or front of queue
    // An item is removed from the Deque and returned
    // If the Deque is empty the return value is undefined.
    // If TRUE then remove from front else from rear.
    { 
       if (count > 0){
           count--;
           if (leftEnd){
    		   front=(front+1)%maxCount;
               return data[front-1];}
           else{
               return data[(count+front)%maxCount];}
       }
    }
    Both are defined in Deque.cpp.
    When I call B.add( A.remove() ) it works but B.add( A.remove(), TRUE) gives a Bus error -A is defined as before, with 40 items.

    I have looked at many webpages about how a Deque is supposed to work (including http://gcc.gnu.org/onlinedocs/libstd...ce.html#l00866 which gives an in-depth explanation of the STL Deque) but no help. I am completely stuck at this point. My guess is that add(item, TRUE) is causing the problem. I do really appreciate any help.
    Last edited by Opel_Corsa; 02-17-2006 at 11:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM