954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pointers and Dynamic Arrays

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.

Attachments assignment6b1.cpp (4.41KB) assign6b_2_sample_output.doc (195.5KB)
konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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

proghelper
Light Poster
28 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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++];

Ancient Dragon
Retired & Loving It
Team Colleague
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

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???

konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
void list::add_item(double number)
{
	if (size == max )
	{
               // reallocate the array 
               ArrayPtr m = new ArrayPtr [max + MAX_LIST_SIZE];
              // copy existing data into new array
               memcpy(m, list, size * sizeof(double));
              // delete old array object
              delete[] list;
              // reset list
              list = m;
              // bump array size
              max += MAX_LIST_SIZE;
        }
       // add number to the list
	list [size] = number;
	size++;
}


I'm not getting it. Once I check the size, add it and then count.....else just add the record???[/QUOTE]

Ancient Dragon
Retired & Loving It
Team Colleague
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You