fork download
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. double* getSubArray(double*, int, int);// Declare a function that will get the sub array
  6.  
  7. int main()
  8. {
  9. const int size = 100;// Declare the size of the array
  10. double* pA;// Declare the variable to hold the pointers to the data in array
  11. double* pB;
  12.  
  13. int start = 15;
  14. int end = 35;
  15.  
  16. pA = new double[size];// Create space for the array
  17. srand(clock());// Seed the program to the computers current time so that random gets a different set of random numbers everytime it is run
  18.  
  19. // Use a for loop to traverse through each element of the array (starting at index 0) placing a number defined by the random function that is no higher than 250
  20. for (int i = 0; i < size; i++)
  21. {
  22. pA[i] = rand()%250;
  23. }
  24.  
  25. cout << "An Array of 100 numbers is created and stored in the heap, these values are:" << endl;
  26. // Output the Array for the user to see
  27. for (int j = 0; j < size; j++)
  28. {
  29. // Place 10 numbers on each line
  30. if (j % 10 == 0)
  31. {
  32. cout << endl;
  33. }
  34. cout << *(pA + j) << " ";
  35. }
  36.  
  37. cout << endl << "The program will build a second array using the data between the indexes " << start << " & " << end << endl;
  38.  
  39. pB = getSubArray(pA, start, end);// Pass the data to the method
  40.  
  41. // Output second array for user to compare
  42. for (int k = 0; k < end-start; k++)
  43. {
  44. // Place 10 numbers on each line
  45. if (k % 10 == 0)
  46. {
  47. cout << endl;
  48. }
  49. cout << *(pB + k) << " ";
  50. }
  51.  
  52. return 0;
  53. }
  54.  
  55. double* getSubArray(double* pA, int start, int end)
  56. {
  57. double* pB = new double[end-start];// Declare space in the heap for the new array whoes size is the size of the criteria given
  58.  
  59. for (int i = start; i < end; i++)
  60. {
  61. pB[i-start] = pA[i];
  62. }
  63.  
  64. return pB;
  65. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
An Array of 100 numbers is created and stored in the heap, these values are:

133 136 27 165 43 85 136 242 149 171 
112 27 190 59 13 176 40 176 172 236 
211 118 67 179 32 30 112 123 67 135 
179 52 22 58 69 167 143 206 11 42 
229 123 171 169 34 37 198 74 65 120 
163 26 91 230 206 123 112 170 246 31 
55 175 84 77 86 5 96 229 63 107 
124 145 82 45 64 117 184 114 43 0 
87 58 26 178 38 84 153 151 4 149 
182 60 176 118 239 12 226 86 94 39 
The program will build a second array using the data between the indexes 15 & 35

176 40 176 172 236 211 118 67 179 32 
30 112 123 67 135 179 52 22 58 69