*Please utilize C++ format when answering questions 46. a) Write a function that
ID: 3626487 • Letter: #
Question
*Please utilize C++ format when answering questions46. a) Write a function that passes in a vector of integers and a single integer and returns how many instances of the single integer occur in the vector of integers. For example, if the vector had the following values, 10, 1, 15, 9, 14, 15, 18, 15, 7, 10, and the single integer value passed in was 15, then the function would return 3 since there are 3 instances of the value 15 in the vector. Another example, if the vector had the values, 1, 4, 9, 2, 8, -15, -3, and the single integer value passed in was 5, then the function would return 0 since there are 0 instances of the value 5 in the vector.
b) Write a function call for the function you just defined. Declare, and initialize if needed,any variables you need to make this function call.
Explanation / Answer
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int count(vector<int>& v,int k)
{
int temp=0;
for(int i=0; i<v.size(); i++)
if(v[i]==k) temp++;
return temp;
}
int main()
{
int a[]={10, 1, 15, 9, 14, 15, 18, 15, 7, 10};
vector<int> v(a,a+sizeof(a)/sizeof(int));
cout << count(v,15)<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.