fork download
  1. // Saliha Babar CS1A Chapter 9, Page 539, #10
  2. //
  3. /*******************************************************************************
  4.  * REVERSE ARRAY
  5.  * _____________________________________________________________________________
  6.  * This program fills an array with random integers and then creates a reversed
  7.  * copy of the array.
  8.  *
  9.  * ____________________________________________________________________________
  10.  * INPUT
  11.  * SIZE : size of an array
  12.  * maxRange : maximum range of random numbers
  13.  * OUTPUT
  14.  * pointer : variable that points towards the start of
  15.  * reversed array
  16.  * ****************************************************************************/
  17.  
  18. #include <iostream>
  19. #include <cstdlib>
  20. #include <cmath>
  21.  
  22. using namespace std;
  23. int *ReverseArray (int array[],int SIZE);
  24.  
  25. int main() {
  26. unsigned seed = time (0); // to generate random numbers
  27. int randomNumber; // integer random number
  28. int maxRange = 100; // INPUT - maximum range of random numbers
  29. const int SIZE = 9; // INPUT - size of an array
  30. int originalArray[SIZE]; // holds random numbers
  31. int *pointer; // OUTPUT - Pointer for reversed copy
  32. //Seed the random number generator
  33. srand(seed);
  34.  
  35. // Fill the array with random numbers
  36. for (int i =0 ;i < SIZE ; i ++)
  37. {
  38. randomNumber = 1 + rand() % maxRange;
  39. originalArray[i] = randomNumber;
  40. }
  41.  
  42. // Pass the original to make a copy as a reverse
  43. pointer = ReverseArray (originalArray,SIZE);
  44.  
  45. cout << "This is the original array of ints \n";
  46. for (int i = 0 ; i < SIZE ; i++)
  47. {
  48. cout << *(originalArray + i) << " ";
  49. }
  50.  
  51. cout << "\nThis is copied and reversed array of ints \n";
  52. for (int i = 0 ; i < SIZE ; i++)
  53. {
  54. cout << *(pointer + i) << " ";
  55. }
  56.  
  57. delete [] pointer;
  58. return 0;
  59. }
  60.  
  61. int *ReverseArray (int array[],int SIZE)
  62. {
  63. // Allocate new memory for the reversed version..
  64. int *reversePointer;
  65.  
  66. reversePointer = new int [SIZE];
  67.  
  68. // Modify the content of reverse pointer with reverse ints..
  69. for (int i = 0; i < SIZE ; i ++)
  70. {
  71. *(reversePointer + SIZE - 1 - i) = *(array + i);
  72. }
  73.  
  74. return reversePointer;
  75. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
This is the original array of ints 
100  31  30  35  57  21  46  64  44  
This is copied and reversed array of ints 
44  64  46  21  57  35  30  31  100