473,788 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.cx x
//
//
//
// 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::seque nce ()
{
current_index = 0;

used = 0;

}

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

}

void sequence::advan ce( )
{
assert (is_item());
current_index++ ;

}

void sequence::inser t(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::attac h(const value_type& entry)
{
int i;
assert(size()<C APACITY);

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

void sequence::remov e_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_it em( ) const
{

return current_index < used;
}

sequence::value _type sequence::curre nt( ) 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 7164
In article <11************ *********@v45g2 000cwv.googlegr oups.com>,
De********@hotm ail.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.cx x
//
//
//
// 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::seque nce ()
{
current_index = 0;

used = 0;

}

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

}

void sequence::advan ce( )
{
assert (is_item());
current_index++ ;

}

void sequence::inser t(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::attac h(const value_type& entry)
{
int i;
assert(size()<C APACITY);

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

void sequence::remov e_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_it em( ) const
{

return current_index < used;
}

sequence::value _type sequence::curre nt( ) 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********@hotm ail.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::remov e_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********@hotm ail.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
3753
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 ============================= #include <vector> #include <list> #include <set> #include <functional>
7
3127
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<> a superior alternative. Why factors am I not considering here? Why in the general case would std::vector<> be best? Thanks, Dave
5
2689
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", "ma", 58, 'g', 64, "ba", 46, 10);}
1
1116
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
1688
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, the behaviours of std::vector::iterator can not meet my requirements. I need to redefine the vector::iterator's behavious such as ++. It means "next position in the object sequence" in STL but now I redefine it to mean "next position within a 2-d...
1
3029
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 array data from 0 to3, I get this {13, JUNK VALUE, 17,38, 18} and JUNK VALUE is like 1.8e831 or something like that. This happens when I use the attach() function and use the current() function to display the values at data I really want to...
4
1642
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 by one using an iterator for the container. I could alternatively use the the std::find or std::find_if algorithms to find an element in a sequence or map respectively. For example, to find an element in a map I could write a function object to...
4
2496
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 Library container. Actaully what's so special in STL containers? What I know is that there will be certain operators overloaded, supports template meta programming, having iterators, compatible with
7
3131
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 "myList" as in the example below ? #include "Sector.h" using namespace boost;
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.