0
0
CppProgramBeginner · 2 min read

C++ Program to Reverse Array with Output and Explanation

To reverse an array in C++, use a loop to swap elements from the start and end moving towards the center, like for (int i = 0; i < n / 2; i++) std::swap(arr[i], arr[n - i - 1]);.
📋

Examples

Inputarr = {1, 2, 3, 4, 5}
Output5 4 3 2 1
Inputarr = {10, 20, 30, 40}
Output40 30 20 10
Inputarr = {7}
Output7
🧠

How to Think About It

To reverse an array, think of swapping the first element with the last, the second with the second last, and so on until you reach the middle. This way, the array elements flip their order without needing extra space.
📐

Algorithm

1
Get the size of the array.
2
Start a loop from the first element to the middle of the array.
3
In each iteration, swap the current element with the element at the corresponding position from the end.
4
Continue until all pairs are swapped.
5
Print the reversed array.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < n / 2; i++) {
        swap(arr[i], arr[n - i - 1]);
    }

    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}
Output
5 4 3 2 1
🔍

Dry Run

Let's trace the array {1, 2, 3, 4, 5} through the code to see how it reverses.

1

Initial array

arr = {1, 2, 3, 4, 5}, n = 5

2

First iteration (i=0)

Swap arr[0] and arr[4]: swap 1 and 5 -> arr = {5, 2, 3, 4, 1}

3

Second iteration (i=1)

Swap arr[1] and arr[3]: swap 2 and 4 -> arr = {5, 4, 3, 2, 1}

4

Loop ends

Middle element arr[2] = 3 stays the same

5

Print reversed array

Output: 5 4 3 2 1

iarr[i]arr[n-i-1]Array after swap
015{5, 2, 3, 4, 1}
124{5, 4, 3, 2, 1}
💡

Why This Works

Step 1: Swapping pairs

Swapping elements at positions i and n - i - 1 reverses the array by exchanging the first with the last, second with second last, and so on.

Step 2: Loop until middle

The loop runs only till the middle because after that, elements would be swapped back to original positions.

Step 3: In-place reversal

The reversal happens inside the original array without extra space, making it memory efficient.

🔄

Alternative Approaches

Using a temporary array
cpp
#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int temp[n];

    for (int i = 0; i < n; i++) {
        temp[i] = arr[n - i - 1];
    }

    for (int i = 0; i < n; i++) {
        arr[i] = temp[i];
    }

    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}
This method uses extra space equal to the array size but is simple to understand.
Using recursion
cpp
#include <iostream>
using namespace std;

void reverseArray(int arr[], int start, int end) {
    if (start >= end) return;
    swap(arr[start], arr[end]);
    reverseArray(arr, start + 1, end - 1);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    reverseArray(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}
Recursion reverses the array by swapping outer elements and calling itself for inner elements; it uses call stack memory.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs n/2 times, which is O(n), because each iteration swaps two elements.

Space Complexity

The reversal is done in-place using constant extra space O(1), no additional arrays needed.

Which Approach is Fastest?

The in-place swap method is fastest and most memory efficient compared to using a temporary array or recursion.

ApproachTimeSpaceBest For
In-place swapO(n)O(1)Memory efficient and fast
Temporary arrayO(n)O(n)Simple but uses extra memory
RecursionO(n)O(n)Elegant but uses call stack memory
💡
Use std::swap to easily exchange two elements without extra variables.
⚠️
Forgetting to stop the loop at the middle causes elements to swap back, resulting in the original array.