fork download
  1. //Andrew Alspaugh CS1A Chapter 9. P. 539. #10
  2.  
  3. /*****************************************************************************
  4. Reverse Order of Elements
  5. ______________________________________________________________________________
  6. This program was created to showcase a function that reverses the order of
  7. elements in the function.
  8.  
  9. The program lets the user enter a size of an array, and enter values for the
  10. elements of the array. The program then uses the ReverseElements function to
  11. reverse the order of elements.
  12.  
  13. The program displays both the original and the reversed array.
  14. _______________________________________________________________________________
  15. //Data Dictionary
  16. //Inputs:
  17. int size;
  18.  
  19. //Outputs:
  20. int count;
  21.  
  22. //dynamic functions
  23. // int *array
  24. // int *reversed
  25.  
  26. ******************************************************************************/
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. int *ReverseElements(int *array, int size);
  31.  
  32. int main()
  33. {
  34. //Data Dictionary
  35. //Inputs:
  36. int size;
  37.  
  38. //Outputs:
  39. int count;
  40.  
  41. //dynamic functions
  42. // int *array
  43. // int *reversed
  44.  
  45. //INPUT
  46. cout << "Enter number of elements: " << endl;
  47. cin >> size;
  48.  
  49. //Create Dynamically Allocated Arrays
  50. int *array = new int[size];
  51. int *reversed = new int[size];
  52.  
  53. //Input values to array
  54. cout << "Enter a value for each element: " << endl;
  55. for (int count = 0; count < size; count++)
  56. cin >> *(array + count);
  57.  
  58. //PROCESS
  59. //Reverse Elements in array and produce array named reverse
  60. reversed = ReverseElements(array, size);
  61.  
  62. //OUTPUT
  63. //output original array
  64. cout << "The original array is: " << endl;
  65. for(int count = 0; count < size; count++)
  66. {
  67. cout << *(array + count) << " ";
  68. }
  69. cout << endl << endl;
  70.  
  71. //output reversed array
  72. cout << "The reversed array is: " << endl;
  73. for(int count = 0; count < size; count++)
  74. {
  75. cout << *(reversed + count) << " ";
  76. }
  77.  
  78. //delete dynamic arrays after use
  79. delete [] array;
  80. delete [] reversed;
  81.  
  82. return 0;
  83.  
  84.  
  85. }
  86.  
  87. //This function reverses the contents in an array
  88. int *ReverseElements (int *array, int size)
  89. {
  90. int *reversed = new int[size];
  91. for(int count = 0; count < size; count++)
  92. {
  93. reversed[count] = array[(size - 1) - count];
  94. }
  95. return reversed;
  96. }
Success #stdin #stdout 0s 5320KB
stdin
10
1124
23434
3123
44687
59876
789
234
964
4567
897
stdout
Enter number of elements: 
Enter a value for each element: 
The original array is: 
1124    23434    3123    44687    59876    789    234    964    4567    897    

The reversed array is: 
897    4567    964    234    789    59876    44687    3123    23434    1124