*please utilize C++ format in answering questions 41. a) Write a function that p
ID: 3626482 • Letter: #
Question
*please utilize C++ format in answering questions
41. a) Write a function that passes in 3 vectors of doubles. The function should fill the 1st vector
with the values of the 2nd and 3rd vectors merged together. Merging for this problem means
the function must take the first value from vector 2 and add it to vector 1, then take the first
value from vector 3 and add it to vector 1, then take the second value from vector 2 and add
it to vector 1, then take the second value from vector 3 and add it to vector 1, and so on until
it takes the last value from vector 2 and then the last value from vector 3. For example, if the
2nd vector contains the values,
1.5, 1.1, 3.0
and the 3rd vector contains the values,
2.0, 2.4, 4.2,
then when the function ends, the 1st vector will contain the values, IN THIS ORDER,
1.5, 2.0, 1.1, 2.4, 3.0, 4.2
The function should just return (do nothing) if the 1
begins or if the 2nd and 3rd vectors are not the same size.
b) Write a main function that tests this function at least once. For the vectors, you may
initialize them with any values you choose, but you must give at least 2 values to each of the
2nd and 3rd vectors being passed into the function.
Explanation / Answer
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void append(vector<double>& v,const vector<double>& v2,const vector<double>& v1)
{
for(int i=0; i<v1.size(); i++)
{
v.push_back(v2[i]);
v.push_back(v1[i]);
}
}
int main()
{
double a[]={2.1, 3.4, 4.5 };
double b[] ={1.0, 1.2, 2.3};
vector<double> v;
vector<double> v2(a,a+3);
vector<double> v1(b,b+3);
append(v,v2,v1);
for(int i=0; i<v.size(); i++)
cout<<v[i] << " " ;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.