-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_an_array.cpp
More file actions
43 lines (35 loc) · 828 Bytes
/
Copy pathreverse_an_array.cpp
File metadata and controls
43 lines (35 loc) · 828 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
#include <iostream>
#include <vector>
using namespace std;
// Function to reverse the array
void reverseArray(vector<int>& arr) {
int start = 0, end = arr.size() - 1;
while (start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}
// Function to print the array
void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}
int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Original array: ";
printArray(arr);
reverseArray(arr);
cout << "Reversed array: ";
printArray(arr);
return 0;
}