Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- using namespace std;
- vector<int> sub_vector(vector <int> v){
- vector <int> sub;
- for (int i = 0; i < v.size(); i++){
- if (v[i]>0){
- sub.push_back(v[i]);
- }
- }
- return sub;
- }
- void display(vector <int> v){
- for (int i = 0; i < v.size(); i++){
- cout << v[i] << " ";
- }
- }
- int main(){
- vector<int> whole(10);
- vector<int> greatest;
- whole = { 20, -2, 10, -2, 20, -2, 10, -2, 20, -4 };
- cout << "These are the vector elements:\n";
- display(whole);
- cout << "\n\n";
- greatest = sub_vector(whole);
- cout << "These are all the positive elements in the previous vector compiled\ninto a subvector:\n";
- display(greatest);
- cout << "\n\n";
- return 0;
- // i wrote a loop that checks each element in the vector. if the vector is positive, it will be placed into a new vector.
- // if the element was negative, it was ignored.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement