Here we are performing swap method in orginal array.
Input:
5
1 2 3 4 5
Output:
5 4 3 2 1
Solution:
//Reverse Array
#include<iostream>
using namespace std;
void print_array(int arr[], int size){
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void reverse_array(int arr[], int start, int end){
while (start<end) // 5 4 3 2 1
{ // 1 2 3 4 5
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main(){
int n;
cin>>n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
cout<<"Previous Array"<<endl;
print_array(arr,n);
reverse_array(arr, 0, n-1);
cout<<"Reverse Array"<<endl;
print_array(arr, n);
return 0;
}
Video Solution :
0 Comments