/* Solution to 3rd question on Tutorial sheet for week 5 */ #include #include /* The 'point' of this question is to get you to do a little * bit of work with functions (even though its not absolutely * necessary to solve the problem). We haven't done much * work on functions that return more than one item, therefore * in coming up with the function, we need to pass at least * some of our solution through a parameter (which therefore * must be in pointer form). * Our choice is to have the return type of the function * be double, and be used to return the mean of the given * parameters. Then we set up a parameter called sd, a * pointer to a variable which will eventually store the * standard deviation. */ double square(double a) { return a*a; } double stats(double *sdpointer, double a1, double a2, double a3) { double mu, sd; mu = (a1 + a2 + a3)/3; sd = (square(a1-mu) + square(a2-mu) + square(a3-mu))/3; *sdpointer = sd; return mu; } int main(void) { printf("\n"); double a, b, c, mean, std; printf("Input three floating point numbers separated by whitespace: "); scanf("%lf", &a); scanf("%lf", &b); scanf("%lf", &c); printf("\n"); mean = stats(&std, a, b, c); printf("The mean is %f and the standard deviation is %f.\n", mean, std); return EXIT_SUCCESS; }