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.

I tried to run on it, but it doesnt work.....what's going on??
please let me. thanks

#include "stdafx.h"
#include <iostream>
using namespace std;

int *duplicateArray(const int *, int);
void displayArray(int[], int);

int _tmain(int argc, _TCHAR* argv[])
{
	// Define constants for the array sizes.
	const int SIZE1 = 5, SIZE2 = 7, SIZE3 = 10;
	
	// Define three arrays of different sizes.
	int array1[SIZE1] = { 100, 200, 300, 400, 500};
	int array2[SIZE2] = { 10, 20, 30, 40, 50, 60, 70};
	int array3[SIZE3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	
	// Define three pointers for the duplicate arrays.
	int *dup1, *dup2, *dup3;

	// Duplicate the arrays.
	dup1 = duplicateArray(array1, SIZE1);
	dup2 = duplicateArray(array2, SIZE2);
	dup3 = duplicateArray(array3, SIZE3);
	
	// Display the original arrays.
	cout << " Here is ....." << endl;
	displayArray(array1, SIZE1);
	displayArray(array2, SIZE2);
	displayArray(array3, SIZE3);
	
	// Display the new array.
	displayArray(dup1, SIZE1);
	displayArray(dup2, SIZE2);
	displayArray(dup3, SIZE3);

	delete [] dup1;
	delete [] dup2;
	delete [] dup3;
	dup1 = 0;
	dup2 = 0;
	dup3 = 0;
	return 0;
}

int *duplicateArray(const int *arr, int size)
{
		int *newArray;

		if (size< 0)
			return NULL;

		newArray = new int[size * 2];
	
		// Copy the array's contents to the new array.
		for( int index = 0; index < size; index++)
			for (int index =0; index < size * 2; index++)
				newArray[index] = arr [index];

		// Return a pointer to the new array.
		return newArray;
}

void displayArray(int arr[], int size)
{
		for(int index= 0; index < size * 2; index++)
			cout << arr[index] << " ";
		cout << endl;

}

Edited 6 Years Ago by Nick Evan: Added code-tags

This article has been dead for over six months. Start a new discussion instead.