Please write these problems in C++ format 13. Write a function named remove_outl
ID: 3626515 • Letter: P
Question
Please write these problems in C++ format13. Write a function named remove_outliers that has a vector parameter (vector of
integers) and two single integer parameters. The function should remove all values in the
vector parameter that are not between the values in the 2 integer parameters (inclusive). The
function should also return true if any value is removed from the vector parameter and
return false if no value is removed.
The order that the integer arguments are passed in should not matter. In other words, your
program should perform exactly the same if the 1st integer argument is larger than the 2nd
integer argument, or vice-versa.
For example, when the function is called, if the vector argument has the values,
1, 8, 4, 9, 3, 7, 100, -4, 5, 0
and the two integer arguments have the values 9 and 2 (or 2 and 9), then when the function
returns, the vector argument will have only the following values,
8, 4, 9, 3, 7, 5, (they do NOT need to be in this order upon return)
and the function will return the value true.
You are encouraged to write your own helper functions. You may NOT use the erase
member function of the vector class.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <vector>
using namespace std;
bool remove_outliers(vector<int>&,int,int);
vector<int> eraseit(vector<int>,int);
int main()
{vector <int> v;
int n,a,b,num;
cout<<"how many elements in the vector: ";
cin>>n;
while(n<2)
{cout<<"must be at least 2 how many elements in vector a: ";
cin>>n;
}
for(int i=0;i<n;i++)
{cout<<"enter a number: ";
cin>>num;
v.push_back(num);
}
cout<<"enter a low bound: ";
cin>>a;
cout<<"enter a high bound: ";
cin>>b;
if(remove_outliers(v,a,b))
{cout<<"elements were removed final vector ";
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
}
else
cout<<"nothing was removed from the vector";
cout<<endl;
system("pause");
return 0;
}
bool remove_outliers(vector<int> &v,int a,int b)
{int low=a,high=b;
bool removed=false;
vector<int>v2;
if(high<low)
{low=b;
high=a;
}
for(int i=v.size()-1;i>=0;i--)
{if(v[i]>high||v[i]<low)
{removed=true;
v=eraseit(v,i);
}
}
return removed;
}
vector<int> eraseit(vector<int>v,int n)
{vector<int> v2;
int i;
for(i=0;i<v.size();i++)
{
if(i!=n)
v2.push_back(v[i]);
}
return v2;
}