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

*Please utilize C++ format when answering questions 14. Write a function named a

ID: 3626491 • Letter: #

Question

*Please utilize C++ format when answering questions

14. Write a function named avg_length that has a single vector parameter (vector of strings).
The function should calculate and return the average length of all the strings in the vector.
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.)
Recall that the string class has a member function named length that returns the number of characters in the string object.
15. Write a test harness that tests the avg_length function you wrote for the previous
question. This must be a complete program, including all necessary include directives and
using statements.
To get all 10 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
avg_length function (regardless of whether you actually have the logic errors in your
definition). A good strategy would be to write a test harness for testing someone else's
implementation of the avg_length function.
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.

Explanation / Answer

First you should define a function avg_length that takes a vector of strings (vector vectorOfStrings) as a parameter and returns a double. You should start your function by defining some variables. The first variable will be a double for the total number of words (double totalWords = 0) which will be set to zero. The next variable will be a double containing the number of letters in all of the words (double totalLetters = 0). Now define an if statement that will return 0.0 if the vector size is equal to 0. The next step is to set up a loop. Either a for or while loop will work in this situation. This loop will run from 0 to the size of the vector and the index will increment by one each time (for (int i = 0; i