/****
 *
 * This program computes simple statistics for three real numbers read from
 * standard input.  The statistics computed are the sum of the numbers, the
 * arithmetic mean, and the standard deviation.  The results are output to
 * standard output, in the following form:
 *
 * Sum = 
 * Mean =
 * Standard Deviation =
 *
 * The precise formulae for mean and standard deviation are as defined here:
 *
 *    http://www.gcseguide.co.uk/statistics_and_probability.htm
 *
 * Input numbers must be non-negative.  If a negative number is input, it is
 * treated as an error, and the program terminates without computing stats.
 *
 *
 * Author: Gene Fisher (gfisher@calpoly.edu)
 * Created: 31mar11
 * Last Modified: 3apr11
 *
 */

#include <stdio.h>
#include <math.h>

#define NUM_DATA_POINTS 3               /* Fixed number of data points */

/*
 * Declare the prototypes for functions used in the program.
 */
double compute_sum(double x1, double x2, double x3);
double compute_mean(double x1, double x2, double x3);
double compute_std_dev(double x1, double x2, double x3);

int main () {

    /*
     * Declare the variables used in main.
     */
    double x1, x2, x3;                  /* Input variables */

    /*
     * Prompt the user for the input.
     */
    printf("Enter three real numbers, separated by spaces: ");

    /*
     * Input the numbers.
     */
    scanf("%lf%lf%lf", &x1, &x2, &x3);

    /*
     * Consider any negative input to be an error, and do not perform the
     * calculation if one or more negative inputs is received.
     */
    if ((x1 < 0) || (x2 < 0) || (x3 < 0)) {
        printf("\nAll inputs must be non-negative: exiting.\n");
    }
    else {
	/*
	 * Compute and output the results.
	 */
	printf("Sum = %f\n", compute_sum(x1, x2, x3));
	printf("Mean = %f\n", compute_mean(x1, x2, x3));
	printf("Standard Deviation = %f\n\n", compute_std_dev(x1, x2, x3));
    }

    return 0;

}

/*
 * Return the sum of the given three numbers.
 */
double compute_sum(double x1, double x2, double x3) {
    return x1 + x2 + x3;
}

/*
 * Return the arithmetic mean of the given three numbers.
 */
double compute_mean(double x1, double x2, double x3) {
    return compute_sum(x1, x2, x3) / NUM_DATA_POINTS;
}

/*
 * Return the standard deviation of the given three numbers.
 */
double compute_std_dev(double x1, double x2, double x3) {
    double mean = compute_mean(x1, x2, x3);
    return sqrt((pow(x1 - mean, 2) +
                 pow(x2 - mean, 2) +
                 pow(x3 - mean, 2)) / (NUM_DATA_POINTS - 1 ));
}