fork download
  1. // Torrez, Elaine CS1A Chapter 9, P. 537, #1
  2.  
  3. /********************************************************************************************
  4.  *
  5.  * ALLOCATE INTEGER ARRAY
  6.  *
  7.  * ------------------------------------------------------------------------------------------
  8.  * This program demonstrates a function that dynamically allocates an array of integers.
  9.  * The program asks the user how many integers to store, allocates the array, allows the
  10.  * user to enter the values, and then displays the integers back to the user.
  11.  *
  12.  * ------------------------------------------------------------------------------------------
  13.  * INPUT
  14.  *
  15.  * numElems : Number of integers to store in the array
  16.  * values : Each integer value entered by the user
  17.  *
  18.  * OUTPUT
  19.  *
  20.  * values : The list of integers stored in the dynamically allocated array
  21.  *
  22.  ********************************************************************************************/
  23.  
  24. #include <iostream>
  25. using namespace std;
  26.  
  27. // FUNCTION PROTOTYPE
  28. int *allocateArray(int size); // Dynamically allocates an int array and returns a pointer
  29.  
  30. int main ()
  31. {
  32. int numElems; // Number of elements to allocate
  33. int *numbers = nullptr; // Pointer to the dynamically allocated array
  34.  
  35. // INPUT: Ask user how many integers the array should hold
  36. cout << "How many integers would you like to store? ";
  37. cin >> numElems;
  38.  
  39. // INPUT VALIDATION: Number of elements must be greater than 0
  40. while (numElems <= 0)
  41. {
  42. cout << "Error: Number of elements must be greater than 0. Try again: ";
  43. cin >> numElems;
  44. }
  45.  
  46. // Call function to allocate the array
  47. numbers = allocateArray(numElems);
  48.  
  49. // INPUT: Fill the array with user-entered integers
  50. cout << "\nEnter " << numElems << " integer values:\n";
  51. for (int index = 0; index < numElems; index++)
  52. {
  53. cout << "Value #" << (index + 1) << ": ";
  54. cin >> *(numbers + index); // Store value using pointer notation
  55. }
  56.  
  57. // OUTPUT: Display the integers stored in the array
  58. cout << "\nYou entered:\n";
  59. for (int index = 0; index < numElems; index++)
  60. {
  61. cout << "Element [" << index << "] = " << *(numbers + index) << endl;
  62. }
  63.  
  64. // Free dynamically allocated memory
  65. delete [] numbers;
  66. numbers = nullptr;
  67.  
  68. return 0;
  69. }
  70.  
  71. //*******************************************************************************
  72. // allocateArray
  73. //------------------------------------------------------------------------------
  74. // This function accepts an integer argument indicating the number of elements
  75. // to allocate. It dynamically allocates an array of integers of that size and
  76. // returns a pointer to the first element of the array.
  77. //*******************************************************************************
  78. int *allocateArray(int size)
  79. {
  80. int *ptr = nullptr; // Pointer to the new array
  81.  
  82. // Dynamically allocate the array
  83. ptr = new int[size];
  84.  
  85. return ptr; // Return pointer to the allocated array
  86. }
  87.  
Success #stdin #stdout 0.01s 5284KB
stdin
5
10
20
30
40
50
stdout
How many integers would you like to store? 
Enter 5 integer values:
Value #1: Value #2: Value #3: Value #4: Value #5: 
You entered:
Element [0] = 10
Element [1] = 20
Element [2] = 30
Element [3] = 40
Element [4] = 50