C++ programs to display array elements in reverse order have been shown here. Both of the iterative and recursive approaches have been covered. The algorithm, pseudocode and time complexity of the programs have also been given.
Example:
Given array A[] = {10, 4, 6, 12, 5}
Elements of A[] in reverse order = {5, 12, 6, 4, 10}
Page content(s):
1. Algorithm to display array elements in reverse order
1. Take input of an array $A[~]$ and the size $n$
2. Set $i = n - 1$
3. For each $i$ from $n - 1$ to $0$, dislay $A[i]$
2. Pseudocode to display array elements in reverse order
Input: An array $A[~]$ and size of array $n$
Output: Array elements in reverse order.
1. Procedure reverse($A[~]$, $n$):
2.
3.
4. End Procedure
3. Time complexity to display array elements in reverse order
Time Complexity: O(n)
Here $n$ is the number of elements in the array.
4. Program & output to display array elements in reverse order
4.1. C++ Program & output to display array elements in reverse order using iterative approach
/************************************** alphabetacoder.com C++ program to print elements of array in reverse order using iteration ***************************************/ #include <iostream> using namespace std; int main() { // declare an array int arr[] = {1, 2, 3, 4, 5, 6}; // declare variables int i, length; // calculate size of array length = sizeof(arr) / sizeof(arr[0]); // display the array cout << "Original array: "; for (i = 0; i < length; i++) cout << arr[i] << " "; // new line cout << endl; // display array elements in reverse order cout << "Reverse array : "; for (i = length - 1; i >= 0; i--) cout << arr[i] << " "; return 0; }
Output
Original array: 1 2 3 4 5 6
Reverse array : 6 5 4 3 2 1
4.2. C++ Program to display array elements in reverse order using pointer
/************************************** alphabetacoder.com C++ program to print elements of array in reverse order using pointer ***************************************/ #include <iostream> using namespace std; int main() { // declare an array int arr[] = {1, 2, 3, 4, 5, 6}; // declare variables int i, length, * p; // calculate size of array length = sizeof(arr) / sizeof(arr[0]); // display the array cout << "Original array: "; for (i = 0; i < length; i++) cout << arr[i] << " "; // pointer p points to the last element of array arr p = & arr[length - 1]; // new line cout << endl; // display array elements in reverse // order using pointer p cout << "Reverse array : "; for (i = length - 1; i >= 0; i--) cout << * p-- << " "; return 0; }
4.3. C++ Program to display array elements in reverse order using recursion
/************************************** alphabetacoder.com C++ program to print elements of array in reverse order using recursion ***************************************/ #include <iostream> using namespace std; // display elements of array in reverse // order function takes an array arr and // an index i as arguments void reverse(int arr[], int i) { // exit condition of recursive call if (i < 0) return; // print current element cout << arr[i] << " "; // call function reverse(arr, i - 1); } int main() { // declare an array int arr[] = {1, 2, 3, 4, 5, 6}; // declare variables int i, length; // calculate size of array length = sizeof(arr) / sizeof(arr[0]); // display the array cout << "Original array: "; for (i = 0; i < length; i++) cout << arr[i] << " "; // new line cout << endl; // display array elements in reverse // order using recursive function reverse() // pass array arr and last index of array as // arguments cout << "Reverse array : "; reverse(arr, length - 1); return 0; }