Reverse an Array in C++


Q. Write a C++ program for Reverse an array.

#include <iostream>   
using namespace std;
  
int main()
{  
    int a[25],b[25], size, i,j;
    cout << "Enter number of elements in array: ";
    cin >> size;
    cout << "Enter the integers: ";
    for (i = 0; i < size; i++)
    {
        cin >> a[i];
    }
    cout << "Reverse of an array is  ";
    for(i=0,j=size-1;i <size;i++,j--)
    {
        b[j] = a[i]; 
    }
    for(i=0;i<size;i++)
    {
       cout << b[i] << " "; 
    }
     return 0;    
} 

Q. Write an algorithm to Reverse an array.

1. Start
2. Read the integer `size` (number of elements).
3. Initialize two integer arrays `a` and `b` of length `size`.
4. For `i` from `0` to `size - 1` do:
   - Read the integer element and assign it to `a[i]`.
5. For `i` from `0` to `size - 1` and `j` from `size - 1` down to `0` simultaneously:
   - Assign `b[j] = a[i]`.
6. For `i` from `0` to `size - 1` do:
   - Print `b[i]` followed by a space.
7. End



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.