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
  • (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers...

    (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). The numbers are displayed in the order of their input and separated by exactly one space. (Hint: Read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.) Sample Run Enter...

  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

  • Given an array of integer, please complete a function multiply(). The function accepts the first memory...

    Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example:   multiply([ 1, 2, 3], 3) → 6   multiply([1, 1, 4 ,2], 4) → 8   multiply([7, 0, 0, 7, 0, 7],...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

    Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...

  • Problem 6(15 Points) MARIE Assembly Programming Develop a MARIE program containing a main function which finds the smallest element of an array T is shown below. The passing arguments and one sub...

    Problem 6(15 Points) MARIE Assembly Programming Develop a MARIE program containing a main function which finds the smallest element of an array T is shown below. The passing arguments and one subroutine function called Sindmin findmin function array and the aro the findmin subroutine functon are the beginning e pseudo code for the subroutine elements of Array1 and Arra array size Continue the following MARIE program to find the smallest ORG 100 Jump Start nt findmin (int "addr, int size)...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

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