944,038 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3516
  • C RSS
Aug 20th, 2005
0

Pointers and Dynamic Arrays

Expand Post »
I am having problems with the most important section of this program. I must admit after reading, I understand the concept, but applying it to this particular program is not coming through. I am attaching the source code but the portion I am having problems with is the following:

void add_item(double number)

What I am trying to do is also attached in the sample snapshot. I know what I need to do but have a hard time implementing. I know I should add entries until it reaches the max. Once it is at the max, add the next one but double the maximum. I hope someone can help.
Attached Files
File Type: cpp assignment6b1.cpp (4.4 KB, 31 views)
File Type: doc assign6b_2_sample_output.doc (195.5 KB, 29 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
konacious is offline Offline
20 posts
since Aug 2005
Aug 20th, 2005
0

Re: Pointers and Dynamic Arrays

In your solution void add_item(double number) should be a member of the class.

It should be defined using

void List::add_item(double number) outside the class

Also I noticed that you never use the number parameter within the add_item method.

Does your progam compile?

-------------------------
Programming ( Assignment / Project ) Help
Reputation Points: 10
Solved Threads: 0
Light Poster
proghelper is offline Offline
28 posts
since Jun 2005
Aug 20th, 2005
0

Re: Pointers and Dynamic Arrays

in the default constructure allocate the array with max number of doubles.

function add_item is all f**ked up. change the name to list::add_item and delete all its contents.

if size == MAX_LIST_SIZE then reallocate the array, otherwise just insert the new number into list[size++];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,954 posts
since Aug 2005
Aug 21st, 2005
0

Re: Pointers and Dynamic Arrays

void list::add_item(double number)
{
ArrayPtr *m = new ArrayPtr double [d1];
delete [m];
int i, d1, d2;

if size = MAX_LIST_SIZE
{
for (i = 0; i < d1; i++)
m[i] = new int[d2]

}
else
{
list [size] = number;
size++;
}
}


I'm not getting it. Once I check the size, add it and then count.....else just add the record???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
konacious is offline Offline
20 posts
since Aug 2005
Aug 21st, 2005
0

Re: Pointers and Dynamic Arrays

  1. void list::add_item(double number)
  2. {
  3. if (size == max )
  4. {
  5. // reallocate the array
  6. ArrayPtr m = new ArrayPtr [max + MAX_LIST_SIZE];
  7. // copy existing data into new array
  8. memcpy(m, list, size * sizeof(double));
  9. // delete old array object
  10. delete[] list;
  11. // reset list
  12. list = m;
  13. // bump array size
  14. max += MAX_LIST_SIZE;
  15. }
  16. // add number to the list
  17. list [size] = number;
  18. size++;
  19. }

I'm not getting it. Once I check the size, add it and then count.....else just add the record???[/QUOTE]
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,954 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Find any root of a number
Next Thread in C Forum Timeline: how to set file access time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC