I need help with a homework, i been working in this for the past 2 weeks and can't get it to work. I need to make a function that will allow me to create a dynamic array and them expand the old array of 3 subcript into the new array by dobling the size. here is what I have so far. I have done docen of changes an none works.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int const SIZE=3;
int * resizeArray ( int *arr, int size);
void displayArray (int arr[], int size);
int main ()
{
int oldArray[SIZE]={10, 20, 30};
int * expandArr;
expandArr=resizeArray(oldArray, SIZE);
//Display Original array
cout<<"Here is the original array contents:\n";
displayArray (oldArray, SIZE);
system("PAUSE");
return 0;
}
int * resizeArray (int *arr, int size)
{
cout<<"Starting the function"<<endl;
int newSize=size*=2;
int *newArray;
if (newSize<=0)
{
return NULL;
}
newArray = new int[newSize];
for (int i=0; i<newSize; i++)
{
newArray[i]=0; cout<<newArray[i]<<endl;
}
for (int i=0; i<size; i++)
{
newArray[i]=arr[i]; cout<<newArray[i]<<endl;
}
cout<<"Leaving the function"<<endl;
return newArray;
}
void displayArray (int arr[], int size)
{
cout<<"Just enter the function display array"<<endl;
for (int i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
I did created the new array and initialized to all 0s but for some reason I can't transfer the old elements to the new sizes array. PLEASE HELP!!!!!