fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 9 P. 539 #11
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Expansion of Array
  6.  * ____________________________________________________________________________
  7.  * This program will accept an array of integers the size being the users
  8.  * choice, the program will then produce a new array that is twice as large.
  9.  * The unused elements in the new array will ne initialized with 0.
  10.  * ____________________________________________________________________________
  11.  * Input
  12.  * size :amount of elements featured in original array
  13.  * arr :integers entered by user to make up the original array
  14.  * Output
  15.  * expanded :new array that is oduble the original amount the user entered
  16.  *****************************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. using namespace std;
  20.  
  21. //Function Protoype
  22. int* expandArray(int* arr, int size);
  23.  
  24. int main() {
  25. //Data Dictionary
  26. int size;
  27. int* arr;
  28. int* expanded;
  29.  
  30. //Prompt User
  31. cout << "Enter the size of the array: " << endl;
  32. cin >> size;
  33.  
  34. arr = new int[size];
  35. cout << "Enter " << size << " integers: " << endl;
  36. for(int i = 0; i < size; i++){
  37. cin >> arr[i];
  38. }
  39. //Invoke Function
  40. expanded = expandArray(arr, size);
  41.  
  42. //Display New Array
  43. cout << "Expanded array (twice the size): " << endl;
  44. for(int i = 0; i < size * 2; i++){
  45. cout << expanded[i] << " ";
  46. }
  47. //Free Memory
  48. delete[] arr;
  49. delete[] expanded;
  50.  
  51. return 0;
  52. }
  53.  
  54. //Function Definition
  55. int* expandArray(int* arr, int size){
  56. int* newArr = new int[size * 2];
  57. for(int i = 0; i < size; i++){
  58. newArr[i] = arr[i];
  59. }
  60. for(int i = size; i < size * 2; i++){
  61. newArr[i] = 0;
  62. }
  63. return newArr;
  64. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
1
2
3
4
5
stdout
Enter the size of the array: 
Enter 5 integers: 
Expanded array (twice the size): 
1 2 3 4 5 0 0 0 0 0