Question 2: write a function [7.5]

Write a complete and correct C++ function that fulfills all the requirements below:

  1. The function's return type is float.
  2. The function's name is average.
  3. The function takes three integer parameters, named x, y, and z.
  4. The function displays (prints) the smallest and largest of the three parameter values, then computes and returns their average (mean).
    For example, the call average(2, 1, 10) would display 1 and 5, and return 4.33333.
You do NOT need to write a main routine or a prototype, just the function itself.

SAMPLE SOLUTION

float average(int x, int y, int z)
{
   float smallest, largest, mean;
   mean = (x + y + z) / 3.0; // needs 3.0 not 3 to ensure floating point division
   if (x < y) {
      smallest = x;
      largest = y;
   } else {
      smallest = y;
      largest = x;
   }
   if (z < smallest) {
      smallest = z;
   }
   if (z > largest) {
      largest = z;
   }
   printf("Smallest is %g, largest is %g\n", smallest, largest);
   return mean;
}