I need to write a function that accepts an in array and the array's size as argument. The function should create a new array that is twice the size of the argument array. the function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with - . the function should return a pointer to the new array. So this is the prompt that I need to complete. I get the array that I want in the function but it isn't being brought to the main function.
void moveOver(int *arr, int size)
{
int *arrp=nullptr;
arrp= new int[size + 1];
//arrp[0] = 0;
for (int i = 1; i <= size; i++)
{
arrp[i] = arr[i-1];
}
arr = new int[size + 1];
*arr = *arrp;
for (int i = 0; i < size + 1; i++) {
//cout << *(arr+i) << endl;
cout << arrp[i] << endl;
}
}
int main()
{
const int size = 4;
int arr[size] = { 1,2,3,4 }, *arrptr=nullptr;
arrptr = arr;
moveOver(arr, size);
for (int i = 0; i <=size ; i++) {
cout << arr[i] << endl;
}
return 0;
}
when it print it prints out only 4 indexes instead of the 5 that I need. any advice would be appreciated. Also I suppose to use pointers and array, I can't use vectors