C++ Container (Sequence Class)

D

Defcon2030

<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
$
 
D

Daniel T.

<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?
 
D

Defcon2030

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.
 
D

Daniel T.

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.
 
D

Defcon2030

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?
 
D

Defcon2030

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
 
D

Daniel T.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top