Hi everyone, having some problems with my homework assignment. Assignment is as follows:
Write a function that accepts an int array and the array's size as arguments. 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 initial-ize the unused elements of the second array with 0. The function should return a pointer to the new array.
So far I have:
#include <iostream>
using namespace std;
int * doubleArray(int array[], int _size);
int* doubleArray(int array[], int _size)
{
int arraySize = _size*2;
int * _pointer;
_pointer = new int[arraySize];
My problem lies in the fact that every time i output the contents of the new array with the for loop back down in main, it outputs "2 4 6 8 10 0 0 5 1606416744 32767" I can't seem to figure out why the last three locations in the array are not being assigned 0. I tried putting the same for loop inside the doubleArray function and got the same output, so i know the problems not in the return of the function. Any ideas?
In main, after you double the array, you're printing the old array but using the doubled size. You're overstepping the bounds of the old array, causing you to print garbage. Doing this might have even caused your program to crash.
Also, remember to delete [] the pointer after you're done with it.