C++ Program to Reverse an Array

This article will teach you how to reverse an array entered by the user during runtime in a C++ program. Here is the list of programs for reversing an array:

Print Reverse of an Array in C++

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 built and runs under the 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:

We printed the array from last to first, that is, from 9th to 0th index, to demonstrate the reverse of an array as output.

Reverse an Array in C++

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

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

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 the following user input:

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

c++ reverse an array

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

Reverse an Array using Pointers in C++

Now let's reverse an array using a pointer. We've created a total of three programs using pointers to reverse an array given by the user. This program uses pointers everywhere, from printing to reversing 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, and 60 as its six elements:

reverse an array using pointer c++

Note: The * is called as the value at operator, whereas & is called as the address of operator.

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

Here is another program using a pointer to reverse an array. In this program, the pointer is used only to reverse the array. The printing of the original and reversed arrays is done normally:

#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 the previous program. Now here is the last program that reverses an array using a pointer. This program is based on swapping operations. This program doesn't use any other array like the 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 a Function in C++

This is the end of this article. This program is created using a user-defined function, revArray(), to reverse an array entered by the user. The function takes two arguments. The first argument is an array, and the second 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 the previous program.

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!