Question

1- Complete all the blanks in the function f1() below such that it implements the operations described in the comments. You m

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

1 2 #include<iostream> #include<vector> using namespace std; 3 4 5 6 7 8 9 10 11 12 13 14 15 int fl (vector int> <vec, int *a

int main() = 4; vecl[2] 5; vector int> vecl (3); vector int> vec2 (2); vec1[0] = 3; vec1[1] vec2[0] = 3; vec2[1] 16 17 18 19

n2 = fl (vec2, Garray2); // Should return with wel = {3) and arrayl = {4}, n2 = 1 cont<<Vector vec2: ; for (int i=0;i Wec2.

CODE (for copy):

#include<iostream>
#include<vector>
using namespace std;

int f1(vector<int> &vec, int **arr)
{
int arrlen = vec.size()/2;
*arr = new int[arrlen];
for(int i = arrlen-1; i >= 0; i--){
(*arr)[i] = vec.back();
vec.pop_back();
}
return arrlen;
}

int main()
{
vector<int> vec1(3); vec1[0] = 3; vec1[1] = 4; vec1[2] = 5;
vector<int> vec2(2); vec2[0] = 3; vec2[1] = 4;

int *array1, *array2;
int n1, n2;

n1 = f1(vec1, &array1);
// Should return with vec1 = {3, 4} and array1 = {5}, n1 = 1
/*
cout<<"Vector vec1: ";
for(int i=0;i<vec1.size();i++)
cout<<vec1[i]<<" ";
cout<<endl;
cout<<"Value of n1: "<<n1;
cout<<"\narray1: ";
for(int i=0;i<n1;i++)
cout<<array1[i]<<" ";
*/
  
n2 = f1(vec2, &array2);
// Should return with vec1 = {3} and array1 = {4}, n2 = 1
/*
cout<<"\n\nVector vec2: ";
for(int i=0;i<vec2.size();i++)
cout<<vec2[i]<<" ";
cout<<endl;
cout<<"Value of n2: "<<n1;
cout<<"\narray2: ";
for(int i=0;i<n2;i++)
cout<<array2[i]<<" ";
*/

// Cleanup code
delete[] array1,array2;
vec1.clear();
vec2.clear();
return 0;
}

For output remove the paragraph comments

OUTPUT:

Vector veci: 34 Value of n1: 1 array1: 5 Vector vec2: 3 Value of n2: 1 array2: 4 Process returned © (@xo) Press any key to co

For any doubt please comment.

Add a comment
Know the answer?
Add Answer to:
1- Complete all the blanks in the function f1() below such that it implements the operations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT