I am needing help with programming challenge problem 12. It will compile, however, its does not make the array twice the size of the argument array. The problem says:

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 intialize the unused elements of the second array with 0. The function should return a pointer to the new array.

This is what I have so far:

//This program is used to accept an int array and the array's size as arguments, and then creates a new
//array that is twice the size of the argument array.
#include 
using namespace std;

int* expandSize (int*, int);

int main()
{
const int SIZE = 10; //Number of elements
int ary [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //Used to hold numbers
int* num = ary;

for(int index= 0;indexcout<

num = expandSize(ary, SIZE); //Used to assign num to the memory location of the return

for(int index=0;indexcout<
delete[] num; //Used to delete memory
num = 0; //Used to point the unused elements of the second array with zero.

return 0;
}
//**********************************************************************************************************
//The function expandSize accepts an int array and the array's size as arguments. The function then creates*
//a new array that is twice the size of the argument array. *
//**********************************************************************************************************
int* expandSize(int* arr, int size)
{
int* expandArray=new int[size * 2];

for (int index=0;indexexpandArray[index]=arr[index];

for (int index=size;indexexpandArray[index]=0;

return expandArray;
}

Recommended Answers

All 2 Replies

Your logic is right, you are just being let down by the syntax of your for loops. Check out examples at http://www.cplusplus.com/doc/tutorial/control/.

Also the code you posted should not compile, there are a number of syntax errors (mainly the for loops as I mentioned above). If it is a direct copy/paste, you might want to investigate why it 'compiled' at your end, maybe the wrong source file?

Yes it did compile, I will repost the code. I am not sure why it posted like that.

//This program is used to accept an int array and the array's size as      arguments, and then creates a new
//array that is twice the size of the argument array.
#include <iostream>
using namespace std;

int* expandSize (int*, int);

int main()
{
	const int SIZE = 10;											//Number of elements
	int ary [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};				//Used to hold numbers
	int* num = ary;													

	for(int index= 0;index<SIZE;index++)
		cout<<num[index]<<endl;
	

	num = expandSize(ary, SIZE);	//Used to assign num to the memory location of the return

	for(int index=0;index<SIZE;index++)
		cout<<num[index]<<endl;

	delete[] num;	//Used to delete memory
	num = 0;		//Used to point the unused elements of the second array with zero.

	return 0;
}
//**********************************************************************************************************
//The function expandSize accepts an int array and the array's size as arguments. The function then creates*
//a new array that is twice the size of the argument array.												   *
//**********************************************************************************************************
int* expandSize(int* arr, int size)
{
int* expandArray=new int[size * 2];

for (int index=0;index<size;index++)
expandArray[index]=arr[index];

for (int index=size;index<size*2;index++)
expandArray[index]=0;

return expandArray;
}
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.