Program to Reverse an Array

  • December 12, 2022
  • C++
Accessing Elements

When an array is reversed, the elements of the original array are changed in order. With this method, the first element becomes the last and the last element becomes the last one in the array. However, the procedure keeps going until all characters or array elements are completely inverted.

Program Code:

#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;   
}  

Output:

2 5 90 21 78 34 

Note: also read about Program To find Average Of Array

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *