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!!!!!

Recommended Answers

All 4 Replies

By the way the only thing i need to do is initialize to 0 all the free spaces in the new array.

>>int newSize=size*=2;
OUCH! That equivalent to : size = size * 2; int newSize = size Which means that the variable size is twice its value. You wanted int newSize = size * 2 . Thus leaving the variable size unchanged.

Make sure you call delete [] expandArr at the end of main.

I did fix that but I still need to initialize the empty subscript of the new array with 0s. Any sugestions?

first thing i noticed is that your SIZE is declared as const, which should give problems if you try to use the SIZE var to create a new array of a different size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.