//Andrew Alspaugh CS1A Chapter 9. P.539. #11
/*****************************************************************************
Expand Array
____________________________________________________________________________
This program allows a user to create a an array of any size greater than 0
and create any input for each of the elements.
The programs purpose is to double the array with function ExpandFunction
_____________________________________________________________________________
//DATA DICTIONARY
//inputs
int size;
//dynamic array to store inputs
//int *array = new int[size];
//outputs
int newSize = 0;
int count = 0;
//dynamic array to store expanded array
// int *expanded = new int[newSize];
*****************************************************************************/
#include <iostream>
using namespace std;
//ExpandFunction Prototype
int *ExpandFunction(int *array, int size, int& newSize);
int main()
{
//DATA DICTIONARY
//inputs
int size;
//dynamic array to store inputs
//int *array = new int[size];
//outputs
int newSize = 0;
int count = 0;
//dynamic array to store expanded array
// int *expanded = new int[newSize];
//INPUT
//Create size of array
cout << "Enter Array Size:" << endl;
cin >> size;
while (size < 0)
{
cout << "Invalid: size must be larger than 0" << endl;
cin >> size;
}
//Initialize Elements in Array
int *array = new int[size];
cout << "Enter Values of array:" << endl;
for ( int count = 0; count < size; count++)
cin >> *(array + count);
//Create Variable for size of expanded array
newSize = size * 2;
//PROCESS (expand array)
////////////////////////////////////////////////////////////////////////////
int* expanded = ExpandFunction(array, size, newSize);
////////////////////////////////////////////////////////////////////////////
//OUTPUT
cout << "Expanded Array:" << endl;
for (int count = 0; count < newSize; count++)
cout << expanded[count] << " ";
//Delete Dynamically Alocated Arrays
delete[] array;
delete[] expanded;
return 0;
}
//ExpandFunction Definition
int *ExpandFunction(int *array, int size, int& newSize)
{
newSize = size * 2;
int *expanded = new int[newSize];
for(int count = 0; count < size; count++)
*(expanded + count) = *(array + count);
for(int count = size; count < newSize; count++)
{
expanded[count] = 0;
}
return expanded;
}