The title of this question might be unclear but what I'm trying to do is write a program that takes an array, say {1, 4, 7}, and turn that array into {1, 4, 7, 1, 4, 7}.
#include
using namespace std;
void repeatArray(double* arr, int size)
{
double* newArr = new double[size*2];
double* ptr = newArr;
int counter = 0;
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < size; i++)
newArr[counter] = arr[i];
counter++;
}
arr = ptr;
}
int main()
{
double* myArray = new double[3];
for (int i=0; i<3; i++)
myArray[i] = (i+1)*2;
repeatArray(myArray, 3);
for (int i=0; i<6; i++)
cout << myArray[i] << endl;
delete[] myArray;
myArray = nullptr;
return 0;
}
The above code outputs "2, 4, 6, 6.95327e-310, 6, 6". It should be "2, 4, 6, 2, 4, 6". Any suggestions on what's going wrong here?
