-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReverseAnArrayRecursion.cpp
More file actions
58 lines (44 loc) · 941 Bytes
/
ReverseAnArrayRecursion.cpp
File metadata and controls
58 lines (44 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Reverese An Array
Input elements of an array of size N.
Write a recursive function to reverse its elements and print them also.
*/
#include<iostream>
using namespace std;
int main()
{
int N, A[20];
void reverse(int [], int, int);
cout<<"Enter array size : ";
cin>>N;
if(N>0)
{
cout<<"\nEnter array elements :\n";
for(int i=0; i<N; ++i)
cin>>A[i];
int j=N-1;
reverse(A,N,j);
cout<<"\nReversed array is :\n";
for (int i=0; i<N; ++i)
cout<<A[i]<<" ";
}
else
{
cout<<"\nSize should be atleast greater than or equal to 1\n";
return 0;
}
return 0;
}
int i=0;
void reverse(int A[], int N, int j)
{
if(i<N/2)
{
int t=A[i];
A[i]=A[j];
A[j]=t;
++i;
--j;
reverse(A,N,j);
}
}