-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse An Array
40 lines (40 loc) · 994 Bytes
/
Reverse An Array
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
#include <bits/stdc++.h>
using namespace std;
void reverse(int arr[],int n){
int i=0,j=n-i-1;
while(i<j){
swap(arr[i],arr[j]);
i++;
j--;
}
}
int main() {
// Write C++ code here
int arr[]={1,2,3,4};
reverse(arr,4);
for(int i=0;i<4;i++){
cout<<arr[i];
}
return 0;
}
ANOTHER WAY
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
void reverse(int arr[],int n){
for(int i=0;i<n/2;i++){
int first_element=arr[i];
int second_element=arr[n-i-1];
arr[i]=second_element;
arr[n-i-1]=first_element;
}
}
int main() {
// Write C++ code here
int arr[]={1,2,3,4};
reverse(arr,4);
for(int i=0;i<4;i++){
cout<<arr[i];
}
return 0;
}