// Marvyn Jesse De La Torre CS1A Chapter 9 #11 pg. 539
/*****************************************************************
* EXPAND ARRAY
*_________________________________________________________________
* This program creates a new array twice the size of the original.
* It copies the original values into the new array and fills the
* extra elements with zeros.
*
* INPUT
* size : Number of original array elements (at least 1)
* original : Integer values stored in the original array
*
* OUTPUT
* expanded : New array containing the original values and zeros
*****************************************************************/
#include <iostream>
using namespace std;
int* expandArray(const int* source, int size)
{
int* expanded = new int[size * 2];
for (int index = 0; index < size; ++index)
*(expanded + index) = *(source + index);
for (int index = size; index < size * 2; ++index)
*(expanded + index) = 0;
return expanded;
}
int getPositiveInteger(const char* prompt)
{
int value;
do
{
cout << prompt;
cin >> value;
if (value <= 0)
cout << "Please enter a value greater than zero.\n";
} while (value <= 0);
return value;
}
void displayArray(const int* values, int size)
{
for (int index = 0; index < size; ++index)
cout << *(values + index) << " ";
cout << endl;
}
int main()
{
const int size =
getPositiveInteger("How many integers are in the original array? ");
int* original = new int[size];
for (int index = 0; index < size; ++index)
{
cout << "Enter integer " << index + 1 << ": ";
cin >> *(original + index);
}
int* expanded = expandArray(original, size);
cout << "\nOriginal array: ";
displayArray(original, size);
cout << "Expanded array: ";
displayArray(expanded, size * 2);
delete[] original;
delete[] expanded;
original = nullptr;
expanded = nullptr;
return 0;
}