Signup/Sign In

C++ Program To Reverse An Array In O(n);

This section will discuss the different ways to reverse an array in the C++ programming language. The reverse of an array means to change the order of the given array's elements. This technique reverses the last element of the array into the first one, and the first element becomes the last. However, the process continues until all characters or elements of the array are completely reversed.

For example, the array contains elements like 'H', 'E', 'L', 'L', 'O', and when we reverse all the elements of an array, it returns the inverted array as 'O', 'L', 'L', 'E', 'H'. So, this way, all the characters in the array are reversed.

Reverse An Array In O(n); In C++ Language

Following are the various ways to get the reverse array in the C++ programming language.

  • Reverse an array using for loop
  • Reverse an array using the reverse() function
  • Reverse an array using the user-defined function
  • Reverse an array using the pointers
  • Reverse an array using the Recursion function
#include <iostream>  
#include <algorithm>  
using namespace std;  
  
// declare disp() function   
void disp(int arr1[], int num)  
{  
    int i;  
    // use for loop to iterate the characters  
    for ( i = 0; i < num; i++)  
    {  
        cout << arr1[i] << " ";  
    }  
}  
  
// define reverse() function to reverse the array elements  
void reverse(int arr1[], int num)  
{  
    reverse(arr1, arr1 + num);   
}  
  
int main ()  
{  
    // declare and initialize an array  
    int arr1[] = {34, 78, 21, 90, 5, 2};  
    int num = sizeof(arr1)/sizeof(arr1[0]);  
      
    // call reverse function and pass parameters  
    reverse(arr1, num);  
    disp(arr1, num); /* call disp() function to print the revrse array. */  
      
    return 0;   
}  


2 5 90 21 78 34

#include <iostream>  
using namespace std;  
  
void ArrRev ( int [], int);  
int main ()  
{  
    int arr[50], num, i, j, temp;  
    cout << " Number of elements to be entered: " << endl;  
    cin >> num;  
          
    cout << " Enter the array elements: " << endl;  
      
    // use for loop to enter the elements  
    for ( i = 0; i < num; i++)  
    {  
        cin >> arr[i];  
    }  
    cout << " Elements are: \n";  
    // display entered elements in array  
    for ( i = 0; i < num; i++)  
    {  
        cout << arr[i] << " ";  
    }  
    ArrRev (arr, num); // call function  
      
    cout << " \n The reverse of the given array is: \n";  
    // use for loop to print the reverse array elements  
    for ( i = 0; i < num ; i++)  
    {  
        cout << arr[i] << " ";  
    }  
    cout << endl;  
    return 0;  
}  
  
void ArrRev ( int ar[], int a2)  
{  
    int i, j, temp;  
    j = a2 - 1;  
    for ( i = 0; i < j; i++, j--)  
    {  
        temp = ar[i];  
        ar[i] = ar[j];  
        ar[j] = temp;  
    }  
}  


Number of elements to be entered:
7
Enter the array elements:
45
32
89
21
78
34
65
Elements are:
45 32 89 21 78 34 65
The reverse of the given array is:
65 34 78 21 89 32 45

Conclusion

Here, we have seen how to implement a C++ program to To Reverse An Array In O(n);