Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is going on in this function? Please utilize C++ to extrapolate the process

ID: 3626542 • Letter: W

Question

What is going on in this function? Please utilize C++ to extrapolate the process behind each important part(do the parts that are in bold red font).

#include<iostream>
#include<vector>
using namespace std;
double avg_length(vector<string>& v)
{
double ans=0;
for(int i=0; i<v.size(); i++)
ans = ans + v[i].length();
return ans/v.size();
}
int main()
{
string a[]={"hello", "hi", "goodbye", "adios"};
vector<string> v(a,a+4);
cout << avg_length(v) <<" ";

return 0;
}

Explanation / Answer

please rate - thanks

this what your looking for?

#include<iostream>
#include<vector>
using namespace std;
double avg_length(vector<string>& v) //function avg_length returns a variable of type double
{                                    //and accepts as a parameter a vector whose string
double ans=0;                        //elements can be changed
for(int i=0; i<v.size(); i++)        //for each element in the vector
ans = ans + v[i].length();           //add the number of characters in that element
return ans/v.size();                 //divide the sum of all the characters by the number of elements
}                                    //in other words, find the average length of the elements in the vector
int main()
{
string a[]={"hello", "hi", "goodbye", "adios"};
vector<string> v(a,a+4);       //create a vector from the 5 elements of string array a
cout << avg_length(v) <<" ";   //get, and print,the average length of the elements of the vector

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote