fork download
  1. // Andres Guzman CSC5 Chapter 9, P. 537, #1
  2. /******************************************************************************
  3.  *
  4.  * ALLOCATE ARRAY
  5.  * ____________________________________________________________________________
  6.  * Program will dynamically allocate an array and return a pointer to array.
  7.  *
  8.  * Computation:
  9.  *
  10.  * Recieves array size as an argument and proceeds to dynamically allocate to
  11.  * the value of array size before returning the memory address of the
  12.  * dynamic memory using a pointer.
  13.  * ____________________________________________________________________________
  14.  *
  15.  * INPUT
  16.  *
  17.  * SIZE = Inputed Array Size
  18.  *
  19.  * OUTPUT
  20.  *
  21.  * number = Memory Address of Array
  22.  *
  23.  ******************************************************************************/
  24. #include <iostream>
  25. using namespace std;
  26. int *dma(int a); //Function Prototype
  27. int main()
  28. {
  29. int SIZE; //Variable to hold inputed value
  30. //
  31. //Display Prompt for input
  32. cout << "Please Enter A Value: ";
  33. cin >> SIZE;
  34. while(SIZE <= 0) //Input Validation
  35. {
  36. cout << "Please Enter Positive Value." << endl;
  37. cout << "Please Enter A Value: ";
  38. cin >> SIZE;
  39. }
  40. //
  41. //Calls Functions and adds returned value to variable
  42. int *number = dma(SIZE);
  43. //
  44. //Output results
  45. cout << "\nMemory Address: " << number << endl;
  46. delete [] number;
  47. return 0;
  48. }
  49. /*******************************************************************************
  50.  * Adding definition to function. The function will dynamically allocates an
  51.  * integer array of the size inputed by the user and return a pointer holding
  52.  * the memory address of the allocated array.
  53.  ******************************************************************************/
  54. int *dma(int a)
  55. {
  56. int * arr = new int[a]; //Dynamically Allocates Memory
  57. return arr; //Returns the value of new memory
  58. }
  59.  
Success #stdin #stdout 0s 548KB
stdin
2
stdout
Please Enter A Value: 
Memory Address: 0x55e48094be90