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

C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube...
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube and subtract 12.5
in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
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.
All the answer need to be in C++ 1) Write a class for a "Vector". The...
All the answer need to be in C++ 1) Write a class for a "Vector". The members of the class will be: Data: int data[100]; methods: void inserts(int element); int remove(); int getsize(); bool is empty(): ------------------------------------------------------------------------------------------------------- 2) Using your vector class, create an object using a regular instantiation and a pointer instantiation. What are the advantages and disadvantages of both approaches? ------------------------------------------------------------------------------------------------------- 3) What does the following function do and write a program that uses the function: int foo(int...
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?...
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: 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.
C ++ program that uses a function “IsDivisibleBy5” that returns a 1 if the input integer...
C ++ program that uses a function “IsDivisibleBy5” that returns a 1 if the input integer is evenly divisible by 5. Return 0 otherwise. Also include a function “IsOdd()” that returns a 1 if the input integer is odd. Return 0 otherwise.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT