In this post, we will make a program to reverse an array in c++ using Functions which takes all the elements of the array using for loop from the user, and then we will call our reverse function. We will also print the array in reverse order in C++ using for loop.
How to Reverse an Array in C++ using Functions
#include<iostream>
void rev(int* a,int N);
using namespace std;
int main()
{
int n;
cout<<"\n Enter number of Elements:";
cin>>n;
int * arr=new int[n];
cout<<"\n Enter Array:";
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
cout<<"\n Original Array: ";
for(int i=0; i<n;i++)
cout<<arr[i]<<" ";
rev(arr,n);
return 0;
}
void rev(int* a,int N)
{
int j, temp;
j=N-1;
for(int i=0;i<j;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
cout<<"\n Reversed Array: ";
for(int i=0; i<N;i++)
cout<<a[i]<<" ";
}
Enter the number of Elements:5
Array:11 22 33 44 55
Reversed Array:55 44 33 22 11
Algorithm:
- Take the size of the array.
- Initialize array of given size.
- Input array elements from the user.
- Now, call reverse function by passing array and the size of the array.
- In reverse function, take two variables
i
andj
which will point to starting and ending element of array respectively. - Swap ith element of the array with the jth element until i is less than j.
- Increment
i
by 1 and decreasej
by 1.
Using While loop
In this code, we will reverse the array using while loop in O(n) time complexity.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "\n Enter the size of array:";
cin >> n;
int * arr=new int[n];
cout << "\n Enter an array:";
for (int i = 0; i < n; i++)
cin >> arr[i];
int i=0,j=n-1;
while(i<j)
{
swap(arr[i++],arr[j--]);
}
cout << "\n Reverse array: ";
for (int i=0;i<n;i++)
cout << arr[i] << " ";
}
Reverse array using Standard template library
In this program, we will use inbuilt library of c++ to reverse an array. We have to include an extra header file to use inbuilt reverse function which is algorithm.
reverse( Array_Name+startIndex,Array_Name+endIndex)
: It will reverse the array from starting index to ending index-1.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cout << "\n Enter the size of array:";
cin >> n;
int * arr=new int[n];
cout << "\n Enter an array:";
for (int i = 0; i < n; i++)
cin >> arr[i];
reverse(arr,arr+n);
cout << "\n Reverse array: ";
for (int i=0;i<n;i++)
cout << arr[i] << " ";
}
Reverse an array in C++ without using temp variable
We have not taken any temporary variable to reverse an array in this program. We will print the array in reverse order.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "\n Enter the size of array:";
cin >> n;
int * A=new int[n];
// n is size of array
cout << "\n Enter an array:";
for (int i = 0; i < n; i++)
cin >> A[i];
cout << "\n Reverse array: ";
for (int j = n - 1; j >= 0; j--)
cout << A[j] << " ";
}
Enter the size of the array: 5
Enter an array:1 2 3 4 5
Reverse array:5 4 3 2 1
We did not reverse an array; we just printed the array in reverse order using “for loop” by iterating from the last element to the first element of the array.
Algorithm to reverse an array in C++
- Initialize an integer array.
- Input the size of an array by the user.
- Input elements of an array using for loop.
- Iterate over the array in reverse order.
If you like this program or have any problem/difficulty with this program, then let me know by commenting below or contacting us.
About this program:
Siddharth Jha writes this program. It is one of the simplest ways to print the reverse of an array in c++.
On this site, we most simply upload the source code of different programs. Stay tuned to this site if you are a programmer or want to start coding from scratch. You can also subscribe to our newsletter to get updates about this blog.
i liked this
Thanks