fork download
  1. /*******************************************************************************
  2.  * Aiden Stannard // CS1A // Chapter 9 Homework: Challange 10# // 11/21/2026
  3.  * _____________________________________________________________________________
  4.  *
  5.  * Reverse Array
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program take the values of an array, reverses the order, and outputs the
  9.  * values of the original array and its original order, aswell the new array
  10.  * with the reverse order.
  11.  * _____________________________________________________________________________
  12.  *
  13.  * Input: int* reversed = reverseArray(original, SIZE);
  14.  *
  15.  * Output: Original array: 5 10 15 20 25
  16.  * Reversed array: 25 20 15 10 5
  17.  *
  18.  *
  19.  ******************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. // Function prototype
  24. int *reverseArray(const int arr[], int size);
  25.  
  26. int main() {
  27. const int SIZE = 5;
  28. int original[SIZE] = {5, 10, 15, 20, 25}; // Original function
  29.  
  30. // Call the function and store the returned pointer
  31. int* reversed = reverseArray(original, SIZE);
  32.  
  33. // Display the original array
  34. cout << "Original array: ";
  35. for (int i = 0; i < SIZE; i++) {
  36. cout << original[i] << " ";
  37. }
  38. cout << endl;
  39.  
  40. // Display the reversed array
  41. cout << "Reversed array: ";
  42. for (int i = 0; i < SIZE; i++) {
  43. cout << reversed[i] << " ";
  44. }
  45. cout << endl;
  46.  
  47. // Free the dynamically allocated memory
  48. delete[] reversed;
  49. reversed = nullptr; // reset the pointer to a null value
  50.  
  51. return 0;
  52. }
  53.  
  54. int* reverseArray(const int arr[], int size) {
  55. // Dynamically allocate a new array of the same size
  56. int* newArr = new int[size];
  57.  
  58. // Copy elements in reverse order
  59. for (int i = 0; i < size; i++) {
  60. newArr[i] = arr[size - 1 - i];
  61. }
  62.  
  63. // Return the pointer to the new array
  64. return newArr;
  65. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
Original array: 5 10 15 20 25 
Reversed array: 25 20 15 10 5