Hi guys,

I am currently working on a practical for a first year computing subject. I am determined to work it out because I think it will really unlock pointers and dynamic memory allocation for me. Essentially, the task requires that I produce a program which, when prompted, will store series of numbers in an array, "growing" as necessary.
Here is sample input/output:

How many numbers? 4
Enter the numbers: 2 6 4
How many numbers? 3
Enter the numbers: 23 5 -2
How many numbers? 0
The total numbers entered are: 2 6 4 23 5 -2

I have produced what must be a competitor for "World's Worst Piece of Code". Here it is:

#include <iostream>

using namespace std;

void grow(int array[], int newnumber, int oldsize);

int main()
{

int n;		// how many numbers?

int t;		// the numbers

int current = 0;	// current size of array

bool flag = true;	// flag to control loops

// create initial array

int *array = new int[current];

int j = 0;  // a counter

int count = 0;   // determine whether all of the numbers have been entered

while (true)
{
cout << "How many numbers?" << endl;

cin >> n;

if (n <= 0)
{
for (int i = 0; i < current; i++)
cout << array[i] << endl;
break;
}

grow(array, n, current);

cout << "Enter the numbers?" << endl;

while (cin >> t && flag == true)
{
array[j] = t;
count++;
if (count == n)
flag = false;
}
}

return 0;

}

void grow(int array[], int newnumber, int oldsize)
{
int newsize = oldsize + newnumber; // determine size of new array

int *temp = new int[newsize];

// copy old data into temp

for (int i = 0; i < oldsize; i++)
temp[i] = array[i];

// set the newly added elements to zero

for (int i = oldsize; i < newsize ;i++)
temp[i] = 0;

// delete array

delete [] array;

array = temp;   // swap contents back to array
}

I guess there are a couple of issues going on here:
1 - The ability of the grow function to perform properly.
2 - The way that I have used the control structures, which I know is *terrible*.

I really am keen to improve my understanding, so any help would be greatly appreciated.

Cheers,

Daniel

I dont think you can grow the size of an array in run-time using normal variables.

It basically needs vectors to do that.

Correct me if i am wrong.

Correct me if i am wrong.

Ok: click

But you're right. Vectors would be the best solution.

@Daniel: What does this code do? Does it compile? Does it give runtime errors? Please be more specific.
Also when posting code, please use

[/code][/noparse]

An alternative could be using link list if you want to do it without STL. That way your first issue would be solved, but still the best way would be using vectors.

Thanks for all the quick responses!!! Thanks niek_e, the link was very helpful and it seems like I am on the right track. I will mark this as solved because I think what I am struggling with is not the dynamic memory but some pretty tortuous control structures which, for the moment, are hampering my other efforts.

I absolutely agree that the best way to achieve the functionality of the program would be to use vectors or some other container from the STL. We are actually studying dynamic memory allocation, linked lists, etc. and part of the specs for this particular question involve creating an array and then "growing" it by resizing. I guess that the idea is to understand what's going on behind the scenes. I must say that I am finding linked lists and dynamic memory quite challenging.

Thanks again guys for having a look at my problem.

Cheers,

Daniel

Also, a fundamental problem with your grow() function:

void grow(int array[], int newnumber, int oldsize)
{
//...
array = temp;   // swap contents back to array
}

The last line is completely useless, because "array" is a local variable, assigning it has no effect outside the function.

Also, a fundamental problem with your grow() function:

The last line is completely useless, because "array" is a local variable, assigning it has no effect outside the function.

Hi bugmenot,

My understanding was that arrays are always passed by reference. As a result, I thought that any changes to the array would be carried over outside the function. Is this not the case?

Cheers,

Daniel

> As a result, I thought that any changes to the array would be carried over outside the function.
That's different. Arrays decay into a pointer to the first element and that's why you can change the elements:

#include <iostream>

void foo(int a[])
{
for (int i = 0; i < 5; ++i)
++a[i];
}

int main()
{
int a[] = {1,2,3,4,5};

foo(a);

for (int i = 0; i < 5; ++i)
std::cout << a[i] << '\n';
}

void foo(int a[]) is exactly the same as void foo(int *a) . You can use a to get to the elements of the array. But the pointer is still passed by value, so changing where the pointer points to in the function won't do diddly. ;)

#include <iostream>

int b[] = {5,4,3,2,1};

void foo(int a[])
{
a = b;
}

int main()
{
int a[] = {1,2,3,4,5};

foo(a);

for (int i = 0; i < 5; ++i)
std::cout << a[i] << '\n';
}

Even if it did do something, the compiler would probably complain because you can't reassign arrays. The only reason foo's copy of a can be assigned to is because it has already decayed into a pointer. When foo returns, its copy of the pointer is destroyed and any reassignments you made to it are lost.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.