In: Computer Science
language c++ finish the code and hurry in an exam with less then 30 minutes
#include <iostream>
#include <vector>
using namespace std;
int find(vector<int> & v, int value);
int main(){
vector<int> myVec = {2,30,10,50,100,32,19,43, 201, 311, 99,77,45,15,95, 105, 32, 15};
//complete code here
cout << "First Element is: " << endl;
cout << "Last Element is: " << endl;
cout << "The number of elements is: " << endl;
//Complete the missing code
cout << "Find 201 : result is at index " << endl;
cout << "Find 299 : result is at index " << endl;
return 0;
}
int find(vector<int> & v, int value){
//complete the code here
return -1;
}
return 0;
}
output should be like this
first element is : 2
Last element is :15
the number of element is :18
write a function find that takes in the vector and the value you would like to find
int find(vector<int)> & v, int value)
find 201: result is at index 8
find 299: result is at index -1
C++ CODE:
#include <iostream> #include <vector> using namespace std; int find(vector<int> & v, int value); int main(){ vector<int> myVec = {2,30,10,50,100,32,19,43, 201, 311, 99,77,45,15,95, 105, 32, 15}; //complete code here cout << "First Element is: " << myVec.at(0) << endl; cout << "Last Element is: " << myVec.at(myVec.size() - 1) << endl; cout << "The number of elements is: " << myVec.size() << endl; //Complete the missing code cout << "Find 201 : result is at index " << find(myVec, 201) << endl; cout << "Find 299 : result is at index " << find(myVec, 299) << endl; return 0; } int find(vector<int> & v, int value){ //complete the code here for(int i = 0; i < v.size(); i++){ if(v.at(i) == value) return i; } return -1; }
OUTPUT: