Categories: C++

Program to Reverse an Array

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 weeks ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago