Pointers and Dynamic Arrays

Reply

Join Date: Aug 2005
Posts: 20
Reputation: konacious is an unknown quantity at this point 
Solved Threads: 0
konacious konacious is offline Offline
Newbie Poster

Pointers and Dynamic Arrays

 
0
  #1
Aug 20th, 2005
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, 10 views)
File Type: doc assign6b_2_sample_output.doc (195.5 KB, 12 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 28
Reputation: proghelper is an unknown quantity at this point 
Solved Threads: 0
proghelper proghelper is offline Offline
Light Poster

Re: Pointers and Dynamic Arrays

 
0
  #2
Aug 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Pointers and Dynamic Arrays

 
0
  #3
Aug 20th, 2005
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++];
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: konacious is an unknown quantity at this point 
Solved Threads: 0
konacious konacious is offline Offline
Newbie Poster

Re: Pointers and Dynamic Arrays

 
0
  #4
Aug 21st, 2005
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???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Pointers and Dynamic Arrays

 
0
  #5
Aug 21st, 2005
  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]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC