Question

In: Computer Science

(C++)Put a new integer 1 at the second position of the vector. vec will now look...

(C++)Put a new integer 1 at the second position of the vector. vec will now look like this.

  9 1 0 6 9 10 12 15 20 25 30 80

Your code: ___________________

int bmwOne(vector<int> & myVec)

{ int value = myVec.at(0);
for (auto & x: myVec) {

if (x < value) {

value = x; }

}

return value;

}

Explain the code of bmwOne. What is it doing?

If the function is called from the code below, what will be the output? Write down the full output of this code.

int main(int argc, const char * argv[]) {

vector<int> myVec = {25,15, 20, 11, 3, 10, 6};

cout << "output is: " << mysteryOne(myVec);

return 0;

}

Solutions

Expert Solution

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

int bmwOne(vector<int> & myVec)
{ 
    int value = myVec.at(0);
    for (auto & x: myVec) {
        if (x < value) {
            value = x; 
        }
    }
    return value;
}

int main(int argc, const char * argv[]) {
    vector<int> myVec = {25,15, 20, 11, 3, 10, 6};
    cout << "output is: " << bmwOne(myVec);
    return 0;
}

This code prints the smallest values in vector
So, smallest value is 3
So, output of the code is
output is: 3

############################################

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

void bmwOne(vector<int> & myVec)
{
    vector<int>::iterator it;
    it = myVec.begin();
    it++;
    it = myVec.insert ( it , 1 );
}

int main(int argc, const char * argv[]) {
    vector<int> myVec = {25,15, 20, 11, 3, 10, 6};
    bmwOne(myVec);
    for(int i = 0;i<myVec.size();i++){
        cout<<myVec[i]<<" ";
    }
    return 0;
}

Output of the code is
25 1 15 20 11 3 10 6 


Related Solutions

Put yourself in the position of an entrepreneur who is developing a new product to introduce...
Put yourself in the position of an entrepreneur who is developing a new product to introduce into the market. Briefly describe the product. Then, develop the segmentation, targeting and positioning strategy for marketing the new product. Be sure to discuss (A) the overall strategy(B) Characteristics of the target market (C) Why that target market is attractive, and (D) the positioning stratedy. Provide justification for yout decision.
Exercise 1. An investor has a short position in a European put on a share for...
Exercise 1. An investor has a short position in a European put on a share for $4. The stock price is $40 and the strike price is $41. (a). Suppose now the investor enters also into a long position of put option with strike price $39. This put is on the same underlying and has the same maturity time. Describe the total payoff to the trader, via a payoff table or payoff diagram.
1. The position of a particle moving in a straight line during a 5–second trip is...
1. The position of a particle moving in a straight line during a 5–second trip is s(t) = 2t2 − 2t + 2 cm. Find a time t at which the instantaneous velocity is equal to the average velocity for the entire trip beginning at t = 0. 2. a) A particle moving along a line has position s(t) = t4 − 34t2 m at time t seconds. At which non negative times does the particle pass through the origin?...
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
1. Read the case study below, and put yourself in Maria's position to answer these questions:...
1. Read the case study below, and put yourself in Maria's position to answer these questions: What is the ethical issue that Maria faces? Who benefits/loses if she decides to do as Hans says? What would you do if you were in her shoes? What does this decision reveal about you? Cosmetic Applications Topic: Package Labeling and Advertising Characters: Hans, Cosmetics Group Product Marketing Manager Maria, Assistant Marketing Communications Manager Maria is an Assistant Marketing Communications Manager with TruBlush Cosmetics,...
1. Look Ahead Co. has a defined benefit pension plan that was put into place five...
1. Look Ahead Co. has a defined benefit pension plan that was put into place five years ago. Specific balances and assumptions as of December 31, 2019 are shown in the table below.  The expected return on plan assets was 12%. PBO, 1/1/2019                      4,100,000 Service costs                           954,000 Interest costs (6%)                 246,000 Payments to retirees           (1,000,000) PBO, 12/31/2019                 4,300,000 Plan assets, 1/1/2019         4,000,000 Actual return                            450,000 Cash contribution                    800,000 Payments to retirees           (1,000,000) Plan assets, 12/31/2019     4,250,000 Calculate pension expense for 2019 Prepare all the necessary journal entries at the end of...
Problem 2. Construct a model of propane. Look down the axis of the C(1)-C(2) bond-C(1) should...
Problem 2. Construct a model of propane. Look down the axis of the C(1)-C(2) bond-C(1) should be the front carbon. Arrange the model in the staggered conformation A. Now, keeping the front carbon, C(1), and its attached hydrogens stationary, make a rotation of 60° about the C(1)-C(2) bond. Sketch Newman projection B of the conformation which results. Rotate another 60° about the C(1)-C(2) bond and sketch Newman projection C of the resulting conformation. Continue rotating 60° until conformations D, E...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT