Exercise 2 Appending vectors 10 points Provide an implementa

Exercise 2: Appending vectors (10 points) Provide an implementation of the appendVector function whose declaration is shown below. Both arguments of the function are vectors of int. The function should modify vector v by appending to it all the elements of w. For example, if v (4, 2, 5) and w (11, 3), then v will become (4, 2, 5, 11, 3) as a result of calling the function. Hint: the vector class has a function called push back that appends values passed into it. void appendVector (vector & v const vector int & W) Write test code that thoroughly tests the function. The test code should use assertions. Exercise 3: Probability of a prime number (10 points)

Solution

#include <iostream>
#include <vector>

using namespace std;
void appendVector(vector<int> & v, const vector<int> & w){
for(int i=0; i<w.size(); i++){
v.push_back(w[i]);
}
}

int main()
{
vector<int> v,w;
v.push_back(4);
v.push_back(2);
v.push_back(5);

w.push_back(11);
w.push_back(3);
appendVector(v, w);
cout<<\"Vector elements are after append: \"<<endl;
for(int i=0; i<v.size(); i++){
cout<<v[i]<<\" \";
}
cout<<endl;

return 0;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                           

Vector elements are after append:                                                                                                                                                                                                                      

4 2 5 11 3

 Exercise 2: Appending vectors (10 points) Provide an implementation of the appendVector function whose declaration is shown below. Both arguments of the functi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site