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

Please write these problems in C++ format 3. Write a test harness that tests a f

ID: 3626507 • Letter: P

Question

Please write these problems in C++ format

3. Write a test harness that tests a function named avg_length. This must be a complete program, including all necessary include directives and using statements. However, you are NOT implementing the avg_length function, only testing it.

To get all 20 points for this question, your test harness (main function) must be thorough. This test harness must be capable of finding ALL potential errors in the logic of the function: double avg_length( const vector<string> &v );
If you do not include all specific test values in your actual code, then you must state (in English is fine) what values you will enter for each test. I should be able to run your program (entering the values you state, if applicable) and tell by the output if the function has a logic or runtime error.
The avg_length function should calculate and return the average length of all the strings in the vector passed in. The function should return an average of 0.0 if the vector is empty.
For example, when the function is called, if the vector argument has the 4 string values, "hello", "hi", "goodbye", "adios" the function should return an average length of 4.75. (19 characters divided by 4 strings is 4.75 characters per string.)
You are NOT implementing the avg_length function. You are only writing the test
harness that will thoroughly test the avg_length function.

hypothetical Rubric:
2 pts: Complete program with no major syntax errors.
1 pt: Correctly declares and initializes vector with at least 1 set of test values.
2 pts: Correctly calls function and correctly uses avg length value in at least 1 valid test.
5 pts: Main function is capable of running ALL tests without restarting the program.
10 pts: Thoroughness of test cases. To get these 10 points your program must either
actually run these tests or be capable of having the user input values to run ALL
of the tests you describe in English. Only tests that can by run by your code will
be counted in this part.

Explanation / Answer

#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;
}