473,414 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

C++ Container (Sequence Class)


<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
6 7142
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


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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
5
by: Gernot Frisch | last post by:
Hi, can I tell e.g. a queue that it should re-allocate 512 elements each time it comes to it's boundaries? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",...
1
by: Daniel Etzold | last post by:
Hi, I have something similar to this: template< typename X, typename Y, typename T = std::map< X, Y > > class A { T _t; }; T may be a sequence of pairs or an associative container, e.g.
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
1
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
4
by: techie | last post by:
I want to find an element in a sequence or map by comparing its value (not the key). I can search for an element with a particular value in a sequence or map by iterating through the elements one...
4
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.