-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseVector.cpp
More file actions
36 lines (31 loc) · 847 Bytes
/
reverseVector.cpp
File metadata and controls
36 lines (31 loc) · 847 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
#include <iostream>
#include <vector>
using namespace std;
int reverse(vector<int> &vec) { // Pass by reference by placing '&' here with vector
int start = 0;
int end = vec.size() - 1;
while (start < end) {
swap(vec[start], vec[end]);
start++;
end--;
}
return 0;
}
int main(){
vector<int> vec;
cout << "Enter number of elements to insert in the vector: ";
int n;
cin >> n;
cout << "Enter the elements of the vector: " << endl;
for(int i = 0; i < n; i++){
int element;
cin >> element;
vec.push_back(element); // Insert elements into the vector
}
cout << "Reversed vector elements are: " << endl;
reverse(vec);
for(int i=0; i< vec.size(); i++){
cout << vec[i] << " "; // Print the reversed vector elements
}
return 0;
}