473,774 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sequence Class

hello,

I have to implement a sequence class, however the header file is
predefined

Expand|Select|Wrap|Line Numbers
  1. class sequence
  2. {
  3. public:
  4. // TYPEDEFS and MEMBER CONSTANTS
  5. typedef double value_type;
  6. typedef std::size_t size_type;
  7. static const size_type DEFAULT_CAPACITY = 30;
  8. // CONSTRUCTORS and DESTRUCTOR
  9. sequence(size_type initial_capacity = DEFAULT_CAPACITY);
  10. sequence(const sequence& source);
  11. ~sequence( );
  12. // MODIFICATION MEMBER FUNCTIONS
  13. void resize(size_type new_capacity);
  14. void start( );
  15. void advance( ); //set the current_index to the next number in
  16. the array
  17. void insert(const value_type& entry); //insert before the
  18. number with the current index
  19. void attach(const value_type& entry); //insert after the number
  20. with the current index
  21. void remove_current( );
  22. void operator =(const sequence& source);
  23. // CONSTANT MEMBER FUNCTIONS
  24. size_type size( ) const;
  25. bool is_item( ) const;
  26. value_type current( ) const;
  27. private:
  28. value_type* data; //das array mit den zahlen drinnen
  29. size_type used; //wieviele zahlen in dem array stehen
  30. size_type current_index;
  31. size_type capacity;
  32. };
  33.  
Unfortunately, I guess something is wrong with the copy constructor, or
the insert, attach or resize function:
Expand|Select|Wrap|Line Numbers
  1. sequence::sequence(const sequence& source)
  2. {
  3. data = new value_type[source.capacity];
  4. capacity = source.capacity;
  5. used = source.used;
  6. current_index = source.current_index;
  7. copy(source.data, source.data + used, data);
  8. }
  9.  
Here the insert, attach and resize function:
Expand|Select|Wrap|Line Numbers
  1.  
  2. void sequence::insert(const value_type& entry)
  3. {
  4. if(used == capacity)
  5. resize(used);
  6.  
  7. if(!(is_item()))
  8. current_index = 0;
  9.  
  10. for(int i = used; i current_index; i--)
  11. {
  12. data[i] = data[i-1];
  13. }
  14. data[current_index] = entry;
  15. ++used;
  16. }
  17.  
  18. void sequence::attach(const value_type& entry)
  19. {
  20. if(used == capacity)
  21. resize(used);
  22.  
  23. if(current_index >= used)
  24. {
  25. start();
  26. advance();
  27. advance();
  28. }
  29. if(used!=0)
  30. {
  31. for(int i = used; i current_index+1; i--)
  32. {
  33. data[i] = data[i-1];
  34. }
  35. data[current_index+1] = entry;
  36. current_index++;
  37. }
  38. else
  39. {
  40. data[current_index] = entry;
  41. }
  42. ++used;
  43. }
  44.  
  45. void sequence::resize (size_type new_capacity)
  46. {
  47. value_type* larger_array;
  48.  
  49. if(new_capacity < used)
  50. new_capacity = used;
  51.  
  52. larger_array = new value_type[new_capacity];
  53. copy(data, data + used + current_index, larger_array);
  54. delete[] data;
  55. data = larger_array;
  56. capacity = new_capacity;
  57. }
  58.  
Has anybody an idea what went wrong here? I am working on the problem
fixing for such a long time but I haven't found the error yet...:((

pat

Sep 27 '06 #1
14 4803
pat270881 wrote:
hello,

I have to implement a sequence class, however the header file is
predefined

[large amount of code redacted]

Has anybody an idea what went wrong here? I am working on the problem
fixing for such a long time but I haven't found the error yet...:((
Well, you didn't tell us what you were expecting, or what results you
got that were different from what you were expecting. Tell us that, and
maybe we can help.

Until then, you have an error on line 42 of your code.
Sep 27 '06 #2
pat270881 wrote:
I have to implement a sequence class, however the header file is
predefined
[...]
Unfortunately, I guess something is wrong with the copy constructor, or
the insert, attach or resize function:
[...]
We're not here to do your homework for you (see
http://parashift.com/c++-faq-lite/ho....html#faq-5.2). If you
post a minimal but complete sample that demonstrates what problem
you're talking about (see
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8) and ask
specific questions about the language (not "what's wrong with this?"),
then we can try to help you.

Cheers! --M

Sep 27 '06 #3

Sorry for that I tried to execute this test-program:

[/code]
int test5( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.DEFA ULT_CAPACITY];
size_t i;

// Set up the items array to conatin 1...2*DEFAULT_C APACITY.
for (i = 1; i <= 2*original.DEFA ULT_CAPACITY; i++)
items[i-1] = i;

// Test copying of an empty sequence. After the copying, we change
the original.
cout << "Copy constructor test: for an empty sequence." << endl;
sequence copy1(original) ;
original.attach (1); // Changes the original sequence, but not the
copy.
if (!correct(copy1 , 0, 0, items)) return 0;

// Test copying of a sequence with current item at the tail.
cout << "Copy constructor test: for a sequence with cursor at
tail." << endl;
for (i=2; i <= 2*original.DEFA ULT_CAPACITY; i++)
original.attach (i);
cout << "SIZE: " << original.size() << endl;
sequence copy2(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy2, 2*original.DEFA ULT_CAPACITY,
2*original.DEFA ULT_CAPACITY-1, items)
)
return 0;

// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);
for (i = 1; i < original.DEFAUL T_CAPACITY; i++)
original.advanc e( );
// Cursor is now at location [DEFAULT_CAPACIT Y] (counting [0] as
the first spot).
sequence copy3(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy3, 2*original.DEFA ULT_CAPACITY, original.DEFAUL T_CAPACITY,
items)
)
return 0;

// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);
original.start( );
// Cursor is now at the front.
sequence copy4(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy4, 2*original.DEFA ULT_CAPACITY, 0, items)
)
return 0;

// Test copying of a sequence with no current item.
cout << "Copy constructor test: for a sequence with no current
item." << endl;
original.insert (2);
while (original.is_it em( ))
original.advanc e( );
// There is now no current item.
sequence copy5(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy5, 2*original.DEFA ULT_CAPACITY,
2*original.DEFA ULT_CAPACITY, items)
)
return 0;
}
[/code]

After this line cout << "Copy constructor test: for a sequence with
cursor near middle." << endl; the programm breaks and a pop-window
appears where it is quoted that TestApplication .exe has detected a
problem and has to be finished.

I think the actual error occurs at the original.insert (2); line.
Does andybody know now what went wrong...? :(

pat

Sep 27 '06 #4
pat270881 wrote:
Sorry for that I tried to execute this test-program:

[/code]
int test5( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.DEFA ULT_CAPACITY];
size_t i;

// Set up the items array to conatin 1...2*DEFAULT_C APACITY.
for (i = 1; i <= 2*original.DEFA ULT_CAPACITY; i++)
items[i-1] = i;

// Test copying of an empty sequence. After the copying, we change
the original.
cout << "Copy constructor test: for an empty sequence." << endl;
sequence copy1(original) ;
original.attach (1); // Changes the original sequence, but not the
copy.
if (!correct(copy1 , 0, 0, items)) return 0;

// Test copying of a sequence with current item at the tail.
cout << "Copy constructor test: for a sequence with cursor at
tail." << endl;
for (i=2; i <= 2*original.DEFA ULT_CAPACITY; i++)
original.attach (i);
cout << "SIZE: " << original.size() << endl;
sequence copy2(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy2, 2*original.DEFA ULT_CAPACITY,
2*original.DEFA ULT_CAPACITY-1, items)
)
return 0;

// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);
for (i = 1; i < original.DEFAUL T_CAPACITY; i++)
original.advanc e( );
// Cursor is now at location [DEFAULT_CAPACIT Y] (counting [0] as
the first spot).
sequence copy3(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy3, 2*original.DEFA ULT_CAPACITY, original.DEFAUL T_CAPACITY,
items)
)
return 0;

// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);
original.start( );
// Cursor is now at the front.
sequence copy4(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy4, 2*original.DEFA ULT_CAPACITY, 0, items)
)
return 0;

// Test copying of a sequence with no current item.
cout << "Copy constructor test: for a sequence with no current
item." << endl;
original.insert (2);
while (original.is_it em( ))
original.advanc e( );
// There is now no current item.
sequence copy5(original) ;
original.start( );
original.advanc e( );
original.remove _current( ); // Removes 2 from the original, but not
the copy.
if (!correct
(copy5, 2*original.DEFA ULT_CAPACITY,
2*original.DEFA ULT_CAPACITY, items)
)
return 0;
}
[/code]

After this line cout << "Copy constructor test: for a sequence with
cursor near middle." << endl; the programm breaks and a pop-window
appears where it is quoted that TestApplication .exe has detected a
problem and has to be finished.

I think the actual error occurs at the original.insert (2); line.
Does andybody know now what went wrong...? :(
Sorry. It still isn't minimal but complete. In particular where are the
other functions, such as sequence::start ()?

Regardless of that, however, this is a clear case where you need to
learn to use your debugger to step through the code. In particular,
make sure that you are not attempting to write to a memory location
past the end of your allocated array.

Again, if you have a specific language question, we'd be happy to try
to answer it, but we're not here to do your homework for you.

Cheers! --M

Sep 27 '06 #5

I already tried to debug it and the error was a bad_alloc error so that
I write out of the heap. But I simply do not know why this
happens...?:((

here the other functions of my sequenceimple file:

Expand|Select|Wrap|Line Numbers
  1. sequence::sequence(size_type initial_capacity)
  2. {
  3. data = new value_type[initial_capacity];
  4. capacity = initial_capacity;
  5. used = 0;
  6. current_index = 0;
  7. }
  8.  
  9. sequence::~sequence()
  10. {
  11. delete[] data;
  12. }
  13.  
  14. void sequence::operator = (const sequence& source)
  15. {
  16. value_type* new_data;
  17.  
  18. if (this == &source)
  19. return;
  20.  
  21. if(capacity != source.capacity)
  22. {
  23. new_data = new value_type[source.capacity];
  24. delete[] data;
  25. data = new_data;
  26. capacity = source.capacity;
  27. }
  28.  
  29. used = source.used;
  30. current_index = source.current_index;
  31. copy(source.data, source.data + used, data);
  32. }
  33.  
  34. void sequence::start( )
  35. {
  36. current_index = 0;
  37. }
  38.  
  39. void sequence::advance( )
  40. {
  41. if(is_item())
  42. current_index++;
  43. }
  44.  
  45. void sequence::remove_current( )
  46. {
  47. //cout << current_index << " " << used;
  48. for(int i = current_index; i < used; i++)
  49. {
  50. data[i] = data[i+1];
  51. }
  52. used--;
  53.  
  54. }
  55.  
  56. sequence::value_type sequence::current() const
  57. {
  58. return data[current_index];
  59. }
  60.  
  61. bool sequence::is_item( ) const
  62. {
  63. if(current_index < used)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. return false;
  70. }
  71. }
  72.  
  73. sequence::size_type sequence::size() const
  74. {
  75. return used;
  76. }
  77.  
I know that you should not do my homework but I simply see not the
error I made...:(

Sep 27 '06 #6
pat270881 wrote:
I already tried to debug it and the error was a bad_alloc error so that
I write out of the heap. But I simply do not know why this
happens...?:((
Ahh, here's the rub and finally a C++ language question! The bad_alloc
exception is thrown when the new operator can't allocate storage of the
requested size. In short, your memory allocation is failing. Is the
requested size too large perhaps?

Cheers! --M

Sep 27 '06 #7
Ahh, here's the rub and finally a C++ language question! The bad_alloc
exception is thrown when the new operator can't allocate storage of the
requested size. In short, your memory allocation is failing. Is the
requested size too large perhaps?
Yes I know but I simply do not see where I write out of the heap, that
is my big problem...?:(

Sep 27 '06 #8
pat270881 wrote:
Ahh, here's the rub and finally a C++ language question! The bad_alloc
exception is thrown when the new operator can't allocate storage of the
requested size. In short, your memory allocation is failing. Is the
requested size too large perhaps?

Yes I know but I simply do not see where I write out of the heap, that
is my big problem...?:(
You don't get a bad_alloc exception when you go outside the bounds of
your array (well, in theory, I suppose you could since writing there is
technically undefined behavior which could do anything -- but it's
quite unlikely that it would generate bad_alloc). You only get it when
storage cannot be *allocated*. Put a breakpoint on your "new"
statements, and see if that's where things go off into the weeds.

Cheers! --M

Sep 27 '06 #9


I have already debug the program and the error occurs here:

// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);

but I simply don't know why...? :((

Sep 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1514
by: Chinook | last post by:
OO approach to decision sequence? --------------------------------- In a recent thread (Cause for using objects?), Chris Smith replied with (in part): > If your table of photo data has several types of photos, and you find > yourself saying > > if is_mugshot:
0
1189
by: Neil Waldie | last post by:
I am trying to code a class for use with the XMLSerializer that can produce XML valid for the following schema (fragment): <xs:element name="Group"> <xs:complexType> <xs:sequence minOccurs="1" maxOccurs="unbounded"> <xs:element name="SubElement1" /> <xs:element minOccurs="0" maxOccurs="1" name="SubElement2" /> <xs:element minOccurs="0" maxOccurs="1" name="SubElement3" /> </xs:sequence>
2
2127
by: Eric Lilja | last post by:
I'm looking at an assignment where the students are expected to write a class with a given purpose. According to the assignment, you should be able to do this with instances of the class: ++instance_of_class; instance_of_class++; (++instance_of_class)++; // Isn't this invalid C++? but not: ++(instance_of_class++);
6
7163
by: Defcon2030 | last post by:
<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
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...
7
1949
by: =?iso-8859-2?q?Seweryn_Habdank-Wojew=F3dzki?= | last post by:
Hi There is an example of the code below (mostly written by P. Bindels). In that example is strongly highlighted that very important is the sequence of compiled code. In C++ could be assume that such a sequence exists. But the question is if it is a feature or it is a bug? Especially in the scope of that example we could observe that templates can be somehow separated from its specializations.
0
1550
by: rval | last post by:
I have this schema while teh following sequence <complexType name="SetOfTasks" mixed="true"> <sequence> <choice minOccurs="1" maxOccurs="unbounded"> <element minOccurs="0" ref="tns:task1" /> <element minOccurs="0" ref="tns:task2" /> <element minOccurs="0" ref="tns:task3" /> <element minOccurs="0" ref="tns:task4" /> <element minOccurs="0" ref="tns:task5" />
5
6609
by: Anan18 | last post by:
Hello sir, I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test program to check the correctness of the sequence class. I cannot figure out what the problem is and keep looking back and forth at the textbook and its not helping, can someone please please help me , i cannot figure out the errors in the...
7
2440
by: schickb | last post by:
I think it would be useful if iterators on sequences had the __index__ method so that they could be used to slice sequences. I was writing a class and wanted to return a list iterator to callers. I then wanted to let callers slice from an iterator's position, but that isn't supported without creating a custom iterator class. Are there reasons for not supporting this generally? I realize not all iterators would have the __index__ method,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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
10039
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
9914
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
8937
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
5355
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...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.