Question

Write a value-returning C++ function returns the average length of an array of strings. Name the...

Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header:

double stringsAverageLength(string array [], int n) {
}

where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array.

For example, if the function is called like this:

string cars[3] = { "Toyota", "Ford", "Tesla" };
cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) << endl;

The output would be:

5.00

In C++. thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

double stringsAverageLength(string array[], int n) {
    double total = 0;
    for (int i = 0; i < n; ++i) {
        total += array[i].length();
    }
    return total / n;
}

int main() {
    string cars[3] = { "Toyota", "Ford", "Tesla" };
    cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a value-returning C++ function returns the average length of an array of strings. Name the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • c++ questions Write a function that returns a pointer to the maximum value of an array...

    c++ questions Write a function that returns a pointer to the maximum value of an array of ints: int* ptrToMaximum(int* array, int* end); You don’t have to worry about what happens if end points beyond end(array). Maybe it is nice to allow end to point to before end(array). In the case that (array == end), you should return nullptr. Running the following main function should result in 8 7 being printed to the console. int main() { int a[] =...

  • Question 9 12 pts Let's assume that charlist() is an array of 10000 strings stored in...

    Question 9 12 pts Let's assume that charlist() is an array of 10000 strings stored in the array in the increasing order of their length (shortest string is stored at index 0 and longest one is store at index 9999). Write an efficient function with the following signature that gets the list of strings as its first input parameter and returns a binary number (0 or 1) indicating whether the list contains a string of length n where n is...

  • a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string>...

    a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string> such that the values in the map are the string values in the array of strings, and the keys in the map are the corresponding array indices of the string values. You may assume all necessary libraries have been included in your program and your solution must be syntactically correct in order to receive full credit. map<int, string> arrayToMap(string arr[], int arrSize) { b....

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • Write a function that takes an array of C-strings as a parameter and the number of...

    Write a function that takes an array of C-strings as a parameter and the number of elements currently stored in the array, and returns a single C-string pointer that is the concatenation of all C-strings in the array. Note: The function must take 2 parameters and return "char *". Any other ways will cause some deduction of points.

  • C++ only : Use an integer pointer returning function which would determine the sum of square...

    C++ only : Use an integer pointer returning function which would determine the sum of square of each of two integer pointer variables used as argument of the function. Call the function in the main() using pointer variables and dynamic allocation Question 4: Print both the strings using string double-pointer only in the main() void print(string ** s){ **s = "University of Oregon"; cout<< **s <<endl; } void print(string *x){ *x = "Portland"; cout<<*x<<endl; }

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • C++ question. Please help, thank you! Write a function called averageOdd that returns the average of...

    C++ question. Please help, thank you! Write a function called averageOdd that returns the average of all of the odd numbers in a 2-Dimensional array with 3 columns. If no odd numbers are present, it should return a result of 0. Excessively long solutions that use more than 15 lines of code may lose points. For example, a program that uses the function averageOdd follows: int main() { int data[2][3] = {{3, 1, 4} , {2, 7, 1}}; cout <<...

  • Write a method named avgLength which takes an array of Strings as an input parameter and...

    Write a method named avgLength which takes an array of Strings as an input parameter and returns a double. The method should calculate and return the average length of all the strings in the array (in java langauge)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT