Right then. Firstly, two small points:
1) You are missing a semicolon on your newly written allocation line :p
2)
Strictly speaking, you should check that the user has entered an appropriate value (e.g. not negative and perhaps not too large to prevent excessive memory usage).
Now for the main thing. You are now allocating and deleting your array in main correctly. The remaining issues are within your other function. Here are my suggestions for adjusting that function.
|
int* newArray = new int[size];
|
What is this for? you don't need a second array if all you want is to fill the first one with values.
1 2
|
for(int i = 0; i < size; i++)
array[i] = newArray[i];
|
You assign to each element of array the corresponding element of newArray. However, newArray's values have not been assigned, so they contain whatever happened to be in the memory when it was allocated. Furthermore, you then proceed to change the values of array in the next lines, so I'm not sure what this bit is supposed to achieve.
As far as I can see, all the previous code in this function is completely redundant, but it isn't causing your error. That is due to the following code:
1 2 3 4 5
|
for(int i = 0; i < size; i++)
{
srand(time(0));
array[i] = rand() % 101;
}
|
The numbers generated are not truly random. You keep seeding them with the value from time(0), which is only accurate to a second, I believe. So time(0) will probably yield the same value for each call in this loop. As you are seeding the random generator to the same value before each run, the subsequent call to rand() yields the same value.
Move your call to srand() out of the loop, so that the generator is seeded only once.