fork download
  1. //Andrew Alspaugh CS1A Chapter 9. P. 537. #1
  2.  
  3. /***************************************************************************
  4. Allocate an Array
  5. _____________________________________________________________________________
  6. The purpose of this program is to dynamically allocate an array.
  7. The focus of this program is to use a function to dynamically allocate the array
  8. ______________________________________________________________________________
  9. INPUT:
  10. size: user inputs array size which is passed to the function
  11. OUTPUT:
  12. *array: pointer variable for array starting address
  13. *
  14. *****************************************************************************/
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. int*AllocateArray(int size);
  19.  
  20. int main()
  21. {
  22. //Data Dictionary
  23. //input
  24. int size;
  25. //output
  26. int* array;
  27. int count;
  28. //Input
  29. cout << "Enter number of elements in array" << endl;
  30. cin >> size;
  31.  
  32. //Process
  33. array = AllocateArray(size);
  34.  
  35. //Output
  36. for(int count = 0; count < size; count++)
  37. {
  38. cout << *(array + count) << "\t\t Memory Address is:" << &*(array + count) << endl;
  39. }
  40.  
  41. delete [] array;
  42. return 0;
  43.  
  44. }
  45.  
  46. //Function Definition of Allocate Array, this array creates a new variable for
  47. //array and returns the new memory location
  48. int* AllocateArray (int size)
  49. {
  50. int* array = new int[size];
  51. for (int count = 0; count < size; ++count)
  52. {
  53. array[count] = count + 1;
  54. }
  55. return array;
  56. }
Success #stdin #stdout 0.01s 5288KB
stdin
9
stdout
Enter number of elements in array
1		 Memory Address is:0x56466065fe90
2		 Memory Address is:0x56466065fe94
3		 Memory Address is:0x56466065fe98
4		 Memory Address is:0x56466065fe9c
5		 Memory Address is:0x56466065fea0
6		 Memory Address is:0x56466065fea4
7		 Memory Address is:0x56466065fea8
8		 Memory Address is:0x56466065feac
9		 Memory Address is:0x56466065feb0