fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int* array_factory(int N) {
  7.  
  8. // int A[N]; // static; stored in the stack; transient
  9. int* A = new int[N]; // dynamic; stored in the heap; persistant
  10. for (int i = 0; i < N; i++) {
  11. A[i] = rand() % 201 - 100;
  12. }
  13.  
  14. cout << "工廠開始---" << endl;
  15. for (int i = 0; i < N; i++) {
  16. cout << A[i] << " ";
  17. }
  18. cout << endl;
  19. cout << "工廠結束---" << endl;
  20.  
  21. return A;
  22.  
  23. }
  24.  
  25. int main() {
  26.  
  27. srand(time(0));
  28. int N = 10;
  29. int* A = array_factory(N);
  30.  
  31. for (int i = 0; i < N; i++) {
  32. cout << A[i] << " ";
  33. }
  34. cout << endl;
  35.  
  36. delete [] A; // reclaim the memory space used by A
  37. return 0;
  38.  
  39. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
工廠開始---
-96 -27 77 -59 -81 39 -25 -6 84 99 
工廠結束---
-96 -27 77 -59 -81 39 -25 -6 84 99