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 the array
  7.  *
  8.  * Computation:
  9.  *
  10.  * Receives array size as an argument and dynamically allocates 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. cout << endl;
  41. //
  42. //Calls Functions and adds returned value to variable number
  43. int *number = dma(SIZE);
  44. //
  45. for(int i = 0; i < SIZE; i++)
  46. {
  47. number[i] = (i + 1)*(i + 1);
  48. }
  49. for(int i = 0; i < SIZE; i++)
  50. {
  51. cout << "Line #51: " << number[i] << endl;
  52. }
  53.  
  54. //Output results
  55. cout << "\nMemory Address: " << number << endl;
  56. delete [] number;
  57. for(int i = 0; i < SIZE; i++)
  58. {
  59. cout << "Line #59: " << number[i] << endl;
  60. }
  61. return 0;
  62. }
  63. /*******************************************************************************
  64.  * Adding definition to function. The function will dynamically allocates an
  65.  * integer array of the size inputed by the user and return a pointer holding
  66.  * the memory address of the allocated array.
  67.  ******************************************************************************/
  68. int *dma(int a)
  69. {
  70. int *arr = new int[a]; //Dynamically Allocates Memory
  71. return arr; //Returns the value of new memory
  72. }
  73.  
Success #stdin #stdout 0s 544KB
stdin
20
stdout
Please Enter A Value: 
Line #51: 1
Line #51: 4
Line #51: 9
Line #51: 16
Line #51: 25
Line #51: 36
Line #51: 49
Line #51: 64
Line #51: 81
Line #51: 100
Line #51: 121
Line #51: 144
Line #51: 169
Line #51: 196
Line #51: 225
Line #51: 256
Line #51: 289
Line #51: 324
Line #51: 361
Line #51: 400

Memory Address: 0x561ac3c50e90
Line #59: 0
Line #59: 0
Line #59: -1010577392
Line #59: 22042
Line #59: 25
Line #59: 36
Line #59: 49
Line #59: 64
Line #59: 81
Line #59: 100
Line #59: 121
Line #59: 144
Line #59: 169
Line #59: 196
Line #59: 225
Line #59: 256
Line #59: 289
Line #59: 324
Line #59: 361
Line #59: 400