fork download
  1. //OrinTill Qimid CS1A Chapter 4, P. 294, 10
  2. //
  3. /**************************************************************
  4.  * Dynamic Memory Allocation.
  5.  *
  6.  * ____________________________________________________________
  7.  * Write a function that dynamically allocates an array of integers. The function should
  8.  * accept an integer argument indicating the number of elements to allocate.
  9.  * The function should return a pointer to the array.
  10.  * ____________________________________________________________
  11.  * INPUT
  12.  * Size: the size of the array.
  13.  * OUTPUT
  14.  * array : the array.
  15.  **************************************************************/
  16. #include <iostream>
  17. #include <iomanip>
  18. #include <cmath>
  19. using namespace std;
  20.  
  21. int* allocatearray(int elements)
  22. {
  23. int* array = new int [elements];
  24. if (elements<0)
  25. {
  26. cout<<"Please enter a number greater than 0"<<endl;
  27. }
  28. return array;
  29. }
  30.  
  31. int main()
  32. {
  33. int size;
  34. cout<<"Please enter the size of the array"<<endl;
  35. cin>>size;
  36. int* array = allocatearray(size);
  37. delete array;
  38.  
  39.  
  40.  
  41. return 0 ;
  42. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty