In: Computer Science
(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;
}
#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