I need to transform an array from {4, 2 ,5}
to {4, 2, 5, 4, 2, 5}
. Here's my output: {4, 2, 5, 3.21143e-322, 0, 2}
, which is obviously incorrect. But I cannot seem to figure out the issue in my logic. Perhaps another perspective can find that issue.
This is my code:
void repeatArray(double* oldArray, int size) {
int newSize = size * 2;
double* newArray = new double[newSize];
for (int i = 0; i < size; i++) {
newArray[i] = oldArray[i];
}
for (int i = 0; i < size; i++) {
newArray[size+i] = oldArray[i];
}
oldArray = newArray;
delete [] newArray;
}
int main() {
double* oldArray = new double[3];
oldArray[0] = 4;
oldArray[1] = 2;
oldArray[2] = 5;
repeatArray(oldArray, 3);
for (int i=0; i<6; i++)
cout << oldArray[i] << endl;
delete []oldArray;
return 0;
}