Question 2: write a function [7.5]
Write a complete and correct C++ function that fulfills all the requirements below:
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;
}
|