What is going on in this program? Please utilize C++ to extrapolate the process
ID: 3626543 • Letter: W
Question
What is going on in this program? Please utilize C++ to extrapolate the process behind each important part(do the parts that are in bold red font).
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int count(vector<string>& v,string& st) // EXAMPLE: &v purppose is to...
{
int temp=0;
for(int i=0; i<v.size(); i++)
{
if(v[i].find(st)) temp++;
}
return temp;
}
int main()
{
string st="woo";
string a[] = {"woops", "wood", "wildwooly", "woo", "wonder"};
vector<string> v(a,a+5);
cout<< count(v,st);
return 0;
}
Explanation / Answer
please rate - thanks
#include<iostream>
#include<string>
#include<vector>
using namespace std;
/*the prototype for function count shows that it returns an integer to the
calling program, and accepts 2 parameters. the first is a vector of strings,
that can be changed, the 2nd is a single string that can also be changed */
int count(vector<string>& v,string& st)
{
int temp=0; //temp is a counter that is initioalized to zero
for(int i=0; i<v.size(); i++) //we step through each element of the vector
{
if(v[i].find(st)) temp++; //and add 1 to the counter for each element, of the vector
} //that does not start with the string
return temp; //return the count to the call program
}
int main()
{
string st="woo";
string a[] = {"woops", "wood", "wildwooly", "woo", "wonder"};
vector<string> v(a,a+5); //create vector 5, from the 5 elements of the string array a
cout<< count(v,st); //count how many elements in the vector don't start with the
//string specified in variable st
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.