473,788 Members | 2,810 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
14 4805
pat270881 wrote:
>
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...? :((
No, that's the symptom. Mlimber is telling you that if you see
bad_alloc, that means that new is failing. That's your potential
cause. Don't fixate on what you think is wrong. If you were certain,
you wouldn't have come here. If you ask someone's advice, then listen
to it.

If you're seeing bad_alloc somewhere, that means that new is failing
(not that you're overwriting heap, though that may happen as well). Put
a break point on your new statements, and check that you're passing sane
values as the sizes.

Sep 27 '06 #11
pat270881 wrote:
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...? :((
You are making it too hard to help you. Right now if I wanted to debug
your program, I'd have to collect together several pieces of it from
multiple posts, and then I'd still have to write some code of my own to
have anything that would compile and run.

Your best bet is to post a complete program that demonstrates your
problem. This means that I could copy and paste directly from your
post into my editor, compile, and see your problem (Hint: a complete
program has a main function, and you haven't posted one anywhere).

--
Alan Johnson

Sep 27 '06 #12
pat270881 schrieb:
void sequence::inser t(const value_type& entry)
{
if(used == capacity)
resize(used);

if(!(is_item()) )
current_index = 0;

for(int i = used; i current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = entry;
++used;
}
[...]
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...:((
resize(used);
should be
resize(used+1);

Otherwise you are accessing data[used] in the for-loop, which is one past
the last element.

Why don't you use std::vector?

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 27 '06 #13
Thomas J. Gritzan wrote:
Why don't you use std::vector?
Because this is homework.

Cheers! --M

Sep 28 '06 #14

Sorry, that I cost you so much nerves, that was the error - when a
handed over the used to the resize function I haven't increased the
used, now I use 2*used in insert and attach and now it works :))

Thank you very much for your help!!!

Sep 28 '06 #15

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

Similar topics

7
1517
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
7164
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
6610
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
2441
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
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
10364
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
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...
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...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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();...
1
4069
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
3
2894
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.