By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

C++ Container (Sequence Class)

P: n/a

<bHey, 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[i]= 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[i] = 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[i] = 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];
}
}
-------------------------------
<bWhen 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 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <11*********************@v45g2000cwv.googlegroups. com>,
De********@hotmail.com wrote:
<bHey, 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[i]= 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[i] = 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[i] = 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];
}
}
-------------------------------
<bWhen 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?
Jan 29 '07 #2

P: n/a


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 '07 #3

P: n/a
De********@hotmail.com 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[i] = 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.
Jan 29 '07 #4

P: n/a


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 '07 #5

P: n/a
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 '07 #6

P: n/a
De********@hotmail.com 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.
Jan 30 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.