C++ Container (Sequence Class)

Discussion in 'C++' started by Defcon2030@hotmail.com, Jan 29, 2007.

  1. Guest

    <b> Hey, can someone help me with this? I've been working on it for a
    few days now, and my head's starting to spin... </b>

    // FILE:ex1_imp.cxx
    //
    //
    //
    // CLASS IMPLEMENTED: sequence (see ex1.h for documentation)
    // INVARIANT for the sequence class:
    // 1. The number of items in the sequence is in the member variable
    used;
    // 2. For an empty sequence, we do not care what is stored in any of
    data; for a
    // non-empty sequence the items in the bag are stored in data[0]
    through

    // data[used - 1], and we don't care what's in the rest of data.

    #include <iostream>
    #include <cassert> // Provides assert function
    #include "ex1.h" // With value_type defined as double
    using namespace std;

    namespace main_savitch_3
    {
    // MODIFICATION MEMBER FUNCTIONS
    sequence::sequence ()
    {
    current_index = 0;

    used = 0;

    }

    void sequence::start( )
    {
    current_index = 0;

    }

    void sequence::advance( )
    {
    assert (is_item());
    current_index++;

    }

    void sequence::insert(const value_type& entry)
    {
    int i;

    for (i = used; i > current_index; i--)
    {
    data= data[i-1];
    data[current_index] = entry;
    used++;
    }
    }

    File Edit Options Buffers Tools C++ Help
    void sequence::attach(const value_type& entry)
    {
    int i;
    assert(size()<CAPACITY);

    for (i = used; i > current_index; i--)
    {
    data = data[i+1];
    data[current_index] = entry;
    used++;
    }
    }

    void sequence::remove_current( )
    {
    int i;
    assert (is_item());

    for (i= current_index +1; i < used -1; i++)
    {
    data = data[i+1];
    used--;
    }
    }

    // CONSTANT MEMBER FUNCTIONS
    sequence::size_type sequence::size( ) const
    {
    return used;

    }

    bool sequence::is_item( ) const
    {

    return current_index < used;
    }

    sequence::value_type sequence::current( ) const
    {

    return data[current_index];
    }
    }
    -------------------------------
    <b> When i compile and run the program i get the following output: </
    b>
    ------------------
    $ ex1.out
    I have initialized an empty sequence of real numbers.

    The following choices are available:
    ! Activate the start( ) function
    + Activate the advance( ) function
    ? Print the result from the is_item( ) function
    C Print the result from the current( ) function
    P Print a copy of the entire sequence
    S Print the result from the size( ) function
    I Insert a new number with the insert(...) function
    A Attach a new number with the attach(...) function
    R Activate the remove_current( ) function
    Q Quit this test program
    Enter choice: !

    The following choices are available:
    ! Activate the start( ) function
    + Activate the advance( ) function
    ? Print the result from the is_item( ) function
    C Print the result from the current( ) function
    P Print a copy of the entire sequence
    S Print the result from the size( ) function
    I Insert a new number with the insert(...) function
    A Attach a new number with the attach(...) function
    R Activate the remove_current( ) function
    Q Quit this test program
    Enter choice: +
    ex1_imp.cxx:42: failed assertion `is_item()'
    Abort - core dumped
    $
    , Jan 29, 2007
    #1
    1. Advertising

  2. Daniel T. Guest

    In article <>,
    wrote:

    > <b> Hey, can someone help me with this? I've been working on it for a
    > few days now, and my head's starting to spin... </b>
    >
    > // FILE:ex1_imp.cxx
    > //
    > //
    > //
    > // CLASS IMPLEMENTED: sequence (see ex1.h for documentation)
    > // INVARIANT for the sequence class:
    > // 1. The number of items in the sequence is in the member variable
    > used;
    > // 2. For an empty sequence, we do not care what is stored in any of
    > data; for a
    > // non-empty sequence the items in the bag are stored in data[0]
    > through
    >
    > // data[used - 1], and we don't care what's in the rest of data.
    >
    > #include <iostream>
    > #include <cassert> // Provides assert function
    > #include "ex1.h" // With value_type defined as double
    > using namespace std;
    >
    > namespace main_savitch_3
    > {
    > // MODIFICATION MEMBER FUNCTIONS
    > sequence::sequence ()
    > {
    > current_index = 0;
    >
    > used = 0;
    >
    > }
    >
    > void sequence::start( )
    > {
    > current_index = 0;
    >
    > }
    >
    > void sequence::advance( )
    > {
    > assert (is_item());
    > current_index++;
    >
    > }
    >
    > void sequence::insert(const value_type& entry)
    > {
    > int i;
    >
    > for (i = used; i > current_index; i--)
    > {
    > data= data[i-1];
    > data[current_index] = entry;
    > used++;
    > }
    > }
    >
    > File Edit Options Buffers Tools C++ Help
    > void sequence::attach(const value_type& entry)
    > {
    > int i;
    > assert(size()<CAPACITY);
    >
    > for (i = used; i > current_index; i--)
    > {
    > data = data[i+1];
    > data[current_index] = entry;
    > used++;
    > }
    > }
    >
    > void sequence::remove_current( )
    > {
    > int i;
    > assert (is_item());
    >
    > for (i= current_index +1; i < used -1; i++)
    > {
    > data = data[i+1];
    > used--;
    > }
    > }
    >
    > // CONSTANT MEMBER FUNCTIONS
    > sequence::size_type sequence::size( ) const
    > {
    > return used;
    >
    > }
    >
    > bool sequence::is_item( ) const
    > {
    >
    > return current_index < used;
    > }
    >
    > sequence::value_type sequence::current( ) const
    > {
    >
    > return data[current_index];
    > }
    > }
    > -------------------------------
    > <b> When i compile and run the program i get the following output: </
    > b>
    > ------------------
    > $ ex1.out
    > I have initialized an empty sequence of real numbers.
    >
    > The following choices are available:
    > ! Activate the start( ) function
    > + Activate the advance( ) function
    > ? Print the result from the is_item( ) function
    > C Print the result from the current( ) function
    > P Print a copy of the entire sequence
    > S Print the result from the size( ) function
    > I Insert a new number with the insert(...) function
    > A Attach a new number with the attach(...) function
    > R Activate the remove_current( ) function
    > Q Quit this test program
    > Enter choice: !
    >
    > The following choices are available:
    > ! Activate the start( ) function
    > + Activate the advance( ) function
    > ? Print the result from the is_item( ) function
    > C Print the result from the current( ) function
    > P Print a copy of the entire sequence
    > S Print the result from the size( ) function
    > I Insert a new number with the insert(...) function
    > A Attach a new number with the attach(...) function
    > R Activate the remove_current( ) function
    > Q Quit this test program
    > Enter choice: +
    > ex1_imp.cxx:42: failed assertion `is_item()'
    > Abort - core dumped
    > $


    It does exactly what you programed it to do. When you call 'start()' it
    sets current_index and used to 0, then when you call 'advance()' it
    checks if current_index is less than used, it isn't so the program core
    dumps. Just like it is supposed to.

    What did you want to have happen?
    Daniel T., Jan 29, 2007
    #2
    1. Advertising

  3. Guest

    i'm sorry i pasted the wrong text there.
    what I'm trying to say is:

    when i ask for the size, it tells me some ridiculously large number.
    (actually capacity is 30)
    also, when i tell it to print out a copy of the entire sequence, the
    program crashes.

    Thanks for your help and patience.
    , Jan 29, 2007
    #3
  4. Daniel T. Guest

    wrote:

    > i'm sorry i pasted the wrong text there.
    > what I'm trying to say is:
    >
    > when i ask for the size, it tells me some ridiculously large number.
    > (actually capacity is 30)
    > also, when i tell it to print out a copy of the entire sequence, the
    > program crashes.
    >
    > Thanks for your help and patience.


    void sequence::remove_current( )
    {
    int i;
    assert (is_item());
    const int old_used = used; // added line here
    for (i= current_index +1; i < used -1; i++)
    {
    data = data[i+1];
    used--;
    }
    assert( used == old_used - 1 ); // added line here
    }

    You have other problems along the same lines in other functions. Try to
    be more consistent in your indentation and this sort of problem won't
    happen.
    Daniel T., Jan 29, 2007
    #4
  5. Guest

    alright, i entered:


    assert (is_item() );
    ...... ........
    (for loop)

    assert (used == old_used + 1 );



    to the insert and attach functions, but now those assertions now fail.
    It doesnt seem to be recording the numbers im putting in.
    could there be something wrong with my boolean is_item member
    function?
    , Jan 30, 2007
    #5
  6. Guest

    I just solved the problem. it was a syntax error (the placement of the
    brackets around my for loop)
    the program works perfect now. Thank you for your help!!
    -Ed
    , Jan 30, 2007
    #6
  7. Daniel T. Guest

    wrote:

    > I just solved the problem. it was a syntax error (the placement of the
    > brackets around my for loop)


    Yes, you had your indentation a little confused and that made it look
    like the 'used' variable was only incremented/decremented once, but it
    really was being modified many times.

    > the program works perfect now. Thank you for your help!!


    Glad to hear it.
    Daniel T., Jan 30, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,446
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    510
    Maitre Bart
    Feb 11, 2004
  3. SpOiLeR
    Replies:
    13
    Views:
    592
    Clark S. Cox III
    Mar 4, 2005
  4. stef mientki
    Replies:
    13
    Views:
    616
    stef mientki
    Oct 20, 2007
  5. Leigh Johnston
    Replies:
    7
    Views:
    208
    James Kanze
    Mar 26, 2013
Loading...

Share This Page