1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int *duplicateArray(const int *, int);
void displayArray(int[], int);
int main()
{
const int SIZE1 = 7;
int array1 [SIZE1] = { 5, 10, 15, 20, 25, 30, 35};
int *dup1;
dup1 = duplicateArray(array1, SIZE1);
cout << "Below is the original array:\n";
displayArray(array1, SIZE1);
cout << "\nBelow is the duplicate array:\n";
displayArray(dup1, SIZE1);
delete [] dup1;
dup1 = 0;
return 0;
}
int *duplicateArray(const int *arr, int size)
{
int *newArray;
// Validate the size. If 0 or negative number, pass a null.
if (size <= 0)
return NULL;
newArray = new int[size + 1];
newArray[0] = 0;
for (int index = 0; index + 1 < size; index++)
newArray[index + 1] = arr[index];
return newArray;
}
void displayArray(int arr[], int size)
{
for (int index = 0; index < size ; index++)
cout << arr[index] << " ";
cout <<endl;
}
|