fork download
  1. //Andrew Alspaugh CS1A Chapter 9. P.539. #11
  2.  
  3. /*****************************************************************************
  4. Expand Array
  5. ____________________________________________________________________________
  6. This program allows a user to create a an array of any size greater than 0
  7. and create any input for each of the elements.
  8.  
  9. The programs purpose is to double the array with function ExpandFunction
  10. _____________________________________________________________________________
  11. //DATA DICTIONARY
  12. //inputs
  13. int size;
  14.  
  15. //dynamic array to store inputs
  16. //int *array = new int[size];
  17.  
  18. //outputs
  19. int newSize = 0;
  20. int count = 0;
  21.  
  22. //dynamic array to store expanded array
  23. // int *expanded = new int[newSize];
  24. *****************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. //ExpandFunction Prototype
  29. int *ExpandFunction(int *array, int size, int& newSize);
  30.  
  31. int main()
  32. {
  33. //DATA DICTIONARY
  34. //inputs
  35. int size;
  36.  
  37. //dynamic array to store inputs
  38. //int *array = new int[size];
  39.  
  40. //outputs
  41. int newSize = 0;
  42. int count = 0;
  43.  
  44. //dynamic array to store expanded array
  45. // int *expanded = new int[newSize];
  46.  
  47. //INPUT
  48. //Create size of array
  49. cout << "Enter Array Size:" << endl;
  50. cin >> size;
  51. while (size < 0)
  52. {
  53. cout << "Invalid: size must be larger than 0" << endl;
  54. cin >> size;
  55. }
  56.  
  57. //Initialize Elements in Array
  58. int *array = new int[size];
  59. cout << "Enter Values of array:" << endl;
  60. for ( int count = 0; count < size; count++)
  61. cin >> *(array + count);
  62.  
  63. //Create Variable for size of expanded array
  64. newSize = size * 2;
  65.  
  66. //PROCESS (expand array)
  67. ////////////////////////////////////////////////////////////////////////////
  68. int* expanded = ExpandFunction(array, size, newSize);
  69. ////////////////////////////////////////////////////////////////////////////
  70.  
  71. //OUTPUT
  72. cout << "Expanded Array:" << endl;
  73. for (int count = 0; count < newSize; count++)
  74. cout << expanded[count] << " ";
  75.  
  76. //Delete Dynamically Alocated Arrays
  77. delete[] array;
  78. delete[] expanded;
  79.  
  80. return 0;
  81. }
  82.  
  83. //ExpandFunction Definition
  84. int *ExpandFunction(int *array, int size, int& newSize)
  85. {
  86. newSize = size * 2;
  87.  
  88. int *expanded = new int[newSize];
  89.  
  90. for(int count = 0; count < size; count++)
  91. *(expanded + count) = *(array + count);
  92.  
  93. for(int count = size; count < newSize; count++)
  94. {
  95. expanded[count] = 0;
  96. }
  97. return expanded;
  98. }
Success #stdin #stdout 0.01s 5292KB
stdin
10
7239
391
423
124
582
7239
391
423
124
582
stdout
Enter Array Size:
Enter Values of array:
Expanded Array:
7239 391 423 124 582 7239 391 423 124 582 0 0 0 0 0 0 0 0 0 0