//Dione Torrevillas CS1A Chapter 9, P. 539, #11
/*
********************************************************************
ARRAY EXPANDER
Description: Return a new integer array twice the size of the
original. Copy its values and fill extra positions with 0.
INPUT: Array size and the original integers.
OUTPUT: The expanded array with zeros in its unused positions.
********************************************************************
*/
#include <iostream>
using namespace std;
// Function prototype
int *expandArray(const int *, int);
int main()
{
int size;
cout << "How many integers? ";
cin >> size;
while (size <= 0)
{
cout << "Enter a number greater than 0: ";
cin >> size;
}
int *numbers = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> *(numbers + i);
}
int *expanded = expandArray(numbers, size);
cout << "Expanded array: ";
for (int i = 0; i < size * 2; i++)
cout << *(expanded + i) << " ";
cout << endl;
// Release the dynamically allocated memory.
delete [] numbers;
delete [] expanded;
numbers = 0;
expanded = 0;
return 0;
}
// Return a new array twice as large as the original array.
int *expandArray(const int *original, int size)
{
int *expanded = new int[size * 2];
for (int i = 0; i < size * 2; i++)
{
if (i < size)
*(expanded + i) = *(original + i);
else
*(expanded + i) = 0;
}
return expanded;
}