Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. vector<int> sub_vector(vector <int> v){
  6. vector <int> sub;
  7. for (int i = 0; i < v.size(); i++){
  8. if (v[i]>0){
  9. sub.push_back(v[i]);
  10. }
  11. }
  12. return sub;
  13. }
  14.  
  15. void display(vector <int> v){
  16. for (int i = 0; i < v.size(); i++){
  17. cout << v[i] << " ";
  18. }
  19. }
  20.  
  21. int main(){
  22. vector<int> whole(10);
  23. vector<int> greatest;
  24. whole = { 20, -2, 10, -2, 20, -2, 10, -2, 20, -4 };
  25. cout << "These are the vector elements:\n";
  26. display(whole);
  27. cout << "\n\n";
  28. greatest = sub_vector(whole);
  29. cout << "These are all the positive elements in the previous vector compiled\ninto a subvector:\n";
  30. display(greatest);
  31. cout << "\n\n";
  32. return 0;
  33. // i wrote a loop that checks each element in the vector. if the vector is positive, it will be placed into a new vector.
  34. // if the element was negative, it was ignored.
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement