Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ Question: Write a function named oddsEvens that takes in 3 i... Bookmark

ID: 3571847 • Letter: I

Question

IN C++

Question: Write a function named oddsEvens that takes in 3 i... Bookmark Write a function named oddsEvens that takes in 3 integer vectors. The 1st vector will contain some number of integers and the 2nd and 3rd vectors should be passed in empty. The function should fill the 2nd vector with all of the even values in the 1st vector and fill the 3rd vector with all of the odd values in the 1st vector. The first vector must remain unchanged. For example, if the 1st vector passed in contains the values {6, 5, 2, 10, 11, 4}, then when the function returns, the 2nd vector passed in should contain the values {6, 2, 10, 4} (the even values) and the 3rd vector passed in should contain the values {5, 11} (the odd values). The function should just return (do nothing) if either the 2nd or the 3rd vector are not passed in empty. Give an example of the invocation of this function; you must show the declarations of any variables needed for this purpose, but you do not need to "populate" them - i.e. do not show where they are assigned values.

Explanation / Answer

#include <iostream>
#include <vector>

using namespace std;

void oddsEvens(vector<int>& a,vector<int>& b,vector<int>& c);
void displayVector(vector<int>& a){
vector<int>::const_iterator i;
for(i=a.begin(); i!=a.end(); ++i){
cout<<(*i)<<" ";
}
cout<<endl;
}

int main()
{
int size,num;//size variable to get the number of input, num variable to store the input given by the user
vector<int> a,b,c;// delcaring the 3 vectors of int type
cout<< "Enter the number of elements: ";
cin>> size;//getting the size value
cout<<"Enter the elements: ";
//loop to get the input from the user for size time
for(int i=0;i<size;i++){
cin>>num;
a.push_back(num);// adding the num to the vectors
}
cout<<"displaying the original 1st vector: ";
displayVector(a);//displaying the original 1st vector;
oddsEvens(a,b,c);//invoking the oddsEvens vector
cout<<"displaying the 1st vector after the method: ";
displayVector(a);//displaying the 1st vector after the method;
cout<<"displaying the 2nd vector after the method: ";
displayVector(b);//displaying the original 2nd vector after the menthod;
cout<<"displaying the 3rd vector after the method: ";
displayVector(c);//displaying the original 3rd vector after the menthod;

return 0;
}

void oddsEvens(vector<int>& a,vector<int>& b,vector<int>& c){
if(!b.empty() || !c.empty()){
cout<<"Either second or third vector passed is not empty";
return;
}
int num;
vector<int>::const_iterator i;
for(i=a.begin(); i!=a.end(); ++i){
num= *i;
if(num%2==0){
b.push_back(num);
}else{
c.push_back(num);
}
}
}


-----------output--------
Enter the number of elements: 5   
Enter the elements: 1   
2   
3   
4   
5   
displaying the original 1st vector: 1 2 3 4 5   
displaying the 1st vector after the method: 1 2 3 4 5   
displaying the 2nd vector after the method: 2 4
displaying the 3rd vector after the method: 1 3 5

-----------output--------


Note: Feel free to ask doubts/question. God bless you