Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Container (Sequence Class)

Reply
Thread Tools

C++ Container (Sequence Class)

 
 
Defcon2030@hotmail.com
Guest
Posts: n/a
 
      01-29-2007

<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[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];
}
}
-------------------------------
<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
$

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-29-2007
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[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];
> }
> }
> -------------------------------
> <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?
 
Reply With Quote
 
 
 
 
Defcon2030@hotmail.com
Guest
Posts: n/a
 
      01-29-2007


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.

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      01-29-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
Defcon2030@hotmail.com
Guest
Posts: n/a
 
      01-30-2007


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?



 
Reply With Quote
 
Defcon2030@hotmail.com
Guest
Posts: n/a
 
      01-30-2007
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

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      01-30-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments