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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

1 week ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

3 weeks ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

6 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

6 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

7 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

7 months ago