Reverse an Array
Reverse an Array in C++ (7 Programs With Output)
C++ Reverse an Array Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
// Method 1: Using reverse() from algorithm
int arr1[] = {1, 2, 3, 4, 5};
reverse(arr1, arr1 + n);
// Method 2: Using swap
int arr2[] = {1, 2, 3, 4, 5};
for (int i = 0; i < n / 2; i++) {
swap(arr2[i], arr2[n - i - 1]);
}
cout << "Original array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Reversed (method 1): ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
cout << endl;
cout << "Reversed (method 2): ";
for (int i = 0; i < n; i++) {
cout << arr2[i] << " ";
}
cout << endl;
return 0;
}Original array: 1 2 3 4 5 Reversed (method 1): 5 4 3 2 1 Reversed (method 2): 5 4 3 2 1
Understanding Reverse an Array
This program teaches you how to reverse an array in C++. Reversing an array means changing the order of elements so that the first element becomes the last, the second becomes the second-to-last, and so on. For example, [1, 2, 3, 4, 5] becomes [5, 4, 3, 2, 1]. This is a fundamental array operation used in many algorithms and programming problems.
---
1. What This Program Does
The program reverses the order of elements in an array. For example:
The reversal swaps elements from both ends, moving toward the center until all elements are swapped.
Example:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
---
4. Method 1: Using reverse() Algorithm (STL)
int arr1[] = {1, 2, 3, 4, 5};
reverse(arr1, arr1 + n);
This is the simplest and most recommended method:
How it works:
Advantages:
Example:
int arr[] = {1, 2, 3, 4, 5};
reverse(arr, arr + 5);
---
// arr is now [5, 4, 3, 2, 1]5. Method 2: Using swap() in a Loop
int arr2[] = {1, 2, 3, 4, 5};
for (int i = 0; i < n / 2; i++) {
swap(arr2[i], arr2[n - i - 1]);
}
This method manually swaps elements:
How it works:
Why n/2?
Advantages:
---
6. Other Methods (Mentioned but not shown in code)
Method 3: Using Two Pointers
int left = 0, right = n - 1;
while (left < right) {
swap(arr[left], arr[right]);
left++;
right--;
}
Method 4: Using Recursion
void reverseRecursive(int arr[], int start, int end) {
if (start >= end) return;
swap(arr[start], arr[end]);
reverseRecursive(arr, start + 1, end - 1);
}
Method 5: Using Stack
stack<int> s;
for (int i = 0; i < n; i++) s.push(arr[i]);
for (int i = 0; i < n; i++) {
arr[i] = s.top();
s.pop();
}
Method 6: Using Temporary Array
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];
}
Method 7: Using Vector
vector<int> vec(arr, arr + n);
reverse(vec.begin(), vec.end());
---
// Copy back if needed7. Displaying Results
The program prints:
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
Output:
Both methods produce the same reversed array.
---
cout << "Original array: ";8. Understanding the Reversal Process
Visual Example
(for [1, 2, 3, 4, 5]):
↑ ↑
start end
↑ ↑
start end
↑ ↑
start end
Key Insight
: Only need to swap n/2 pairs. Middle element (if odd length) doesn't move.
---
9. When to Use Each Method
-
reverse() Algorithm
: Best for most cases - simple, optimized, recommended.
-
swap() in Loop
: Good for learning - shows the algorithm clearly.
-
Two Pointers
: Similar to swap method - clear and efficient.
-
Recursion
: Educational - helps understand recursive thinking.
-
Stack
: Demonstrates stack usage - educational purpose.
-
Temporary Array
: When you need to preserve original - uses extra space.
-
Vector
: When working with vectors or need dynamic size.
Best Practice
: Use reverse() for most applications - it's simple, efficient, and works with any container.
---
10. Time and Space Complexity
Time Complexity
:
Space Complexity
:
---
11. Common Use Cases
Algorithm Problems
:
Data Processing
:
Interview Questions
:
---
12. Important Considerations
Array Bounds
:
Odd vs Even Length
:
In-place vs Copy
:
---
13. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning array operations, understanding in-place algorithms, and preparing for more complex array manipulation problems in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Reverse an Array
This C++ program is part of the "Array Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.