C++ Program to Reverse an Array

In this article, you will learn and get code to reverse an array entered by user at run-time in a C++ program. Here are the list of programs on reversing of an array:

  • Print Reverse of an Array without actually Reversing it
  • Reverse an Array and then Print
  • Reverse an Array using Pointer
  • Reverse an Array using User-defined Function

Print Reverse of an Array

This program just prints reverse of an array, without actually reversing it. The question is, write a program in C++ to print reverse of an array. Here is its answer:

#include<iostream>
using namespace std;
int main()
{
    int arr[10], i;
    cout<<"Enter 10 Array Elements: ";
    for(i=0; i<10; i++)
        cin>>arr[i];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<10; i++)
        cout<<arr[i]<<" ";
    cout<<"\n\nThe Reverse of Given Array is:\n";
    for(i=(10-1); i>=0; i--)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program to reverse array

Now supply 10 numbers as 10 elements for an array, one by one, to print its reverse as shown in the sample output given below:

print reverse of array c++

When user enters any 10 numbers say 1, 2, 3, ..., 10, then these numbers gets stored in arr[] in following way:

  • arr[0]=1
  • arr[1]=2
  • arr[2]=3
  • and so on upto
  • arr[9]=10

We've have printed the array from its last index to first, that is its 9th to 0th index to show the reverse of an array as output.

Reverse an Array

This program reverse an array before printing. When the array gets reversed, then when you print the same array, it shows its element in reverse order. This program also allows user to define the size for array.

To reverse an array in C++ programming, you have to ask from user to enter size and then elements (of given size) for the array. Now to reverse, do these things:

  • Move element at first index to last, and element at last index to first
  • Move element at second index to second last, and element at second last index to second
  • and so on

Let's have a look at the program first, its explanation is given later on:

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, j, temp;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    j = tot-1;
    for(i=0; i<j; i++, j--)
    {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    cout<<"\n\nThe Reverse of Given Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

Here is its sample run, with following user input:

  • 5 as size
  • 1, 2, 3, 4, 5 as five array elements

After supplying exactly these inputs, press ENTER key to reverse the array and print both original and its reverse as shown in the snapshot given below:

c++ reverse an array

The dry run of above C++ program with same user input (as provided in previous output) goes like:

  • Initial value, tot=5. Since I've already told about storing of array elements. So arr[0]=1, arr[1]=2, arr[2]=3, arr[3]=4, and arr[4]=5
  • Now the original array gets printed using for loop
  • After printing the original array, tot-1 or 5-1 or 4 gets initialized to j. So j=4
  • Now 0 gets initialized to i and the condition i<j or 0<4 evaluates to be true, therefore program flow goes inside the loop
  • And arr[i] or arr[0] or 1 gets initialized to temp. Then arr[j] or arr[4] or 5 gets initialized to arr[i] or arr[0]. And finally temp or 1 gets initialized to arr[j] or arr[4]
  • A normal swap operation gets performed with three lines of code. Now arr[0]=5 and arr[4]=1. As you can see, element at last index (4th index) gets moved to first index (0th index), and element at first (0th) index gets moved to last (4th) index
  • Now program flow goes to update part of for loop and the value of i gets incremented, whereas the value of j gets decremented. So i=1 and j=3
  • And then the condition again gets evaluated. Because the condition i<j or 1<3 evaluates to be true again, therefore program flow goes inside the loop again
  • This process continues until the condition evaluates to be false
  • In this way, the array gets reversed. So print the array using another for loop to show the reverse of given array

Reverse an Array using Pointers

Now let's reverse an array using pointer. We've created total of three programs using pointer, to reverse an array given by user. This program uses pointer everywhere, from printing to reversing of an array:

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, arrTemp[100], *ptr;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    ptr = &arr[0];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<tot; i++)
    {
        cout<<*ptr<<" ";
        ptr++;
    }
    ptr--;
    for(i=0; i<tot; i++)
    {
        arrTemp[i] = *ptr;
        ptr--;
    }
    ptr = &arrTemp[0];
    for(i=0; i<tot; i++)
    {
        arr[i] = *ptr;
        ptr++;
    }
    ptr = &arr[0];
    cout<<"\n\nThe Reverse of Given Array is:\n";
    for(i=0; i<tot; i++)
    {
        cout<<*ptr<<" ";
        ptr++;
    }
    cout<<endl;
    return 0;
}

Here is its sample run with user input, 6 as size, and 10, 20, 30, 40, 50, 60 as its six elements:

reverse an array using pointer c++

Note - The * is called as value at operator. Whereas & is called as address of operator.

Note - The ptr++ initializes the address of next index. For example, if ptr holds the address of 0th index of an array arr[], then after executing ptr++, now ptr holds the address of 1st index of the same array arr[].

Here is another program using pointer to reverse an array. In this program, pointer is used only to reverse the array. Printing of orignal and reversed array gets performed in a normal way:

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, arrTemp[100], *ptr;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    ptr = &arr[tot-1];
    for(i=0; i<tot; i++)
    {
        arrTemp[i] = *ptr;
        ptr--;
    }
    ptr = &arrTemp[0];
    for(i=0; i<tot; i++)
    {
        arr[i] = *ptr;
        ptr++;
    }
    cout<<"\n\nThe Reverse of Given Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

This program produces the same output as of previous program. Now here is the last program that reverse an array using pointer. This program is created based on swapping operation. This program doesn't use any other array like previous program:

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, j, temp, *ptrOne, *ptrTwo;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    ptrOne = &arr[0];
    ptrTwo = &arr[tot-1];
    j = tot-1;
    for(i=0; i<j; i++, j--)
    {
        temp = *ptrOne;
        *ptrOne = *ptrTwo;
        *ptrTwo = temp;
        ptrOne++;
        ptrTwo--;
    }
    cout<<"\n\nThe Reverse of Given Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}

Reverse an Array using Function

This is the last program of this article. This program is created using a user-defined function revArray() to reverse an array entered by user. The function takes two arguments. The first argument is array, whereas second argument is its size

#include<iostream>
using namespace std;
void revArray(int [], int);
int main()
{
    int arr[100], tot, i, j, temp;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    revArray(arr, tot);
    cout<<"\n\nThe Reverse of Given Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}
void revArray(int a[], int t)
{
    int i, j, temp;
    j = t-1;
    for(i=0; i<j; i++, j--)
    {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

This program does exactly the same job as of previous program.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »