CSC 101 Lecture Notes Week 2
Let's Start Programming
Relevant Reading: Chapters 3 and 4
Compute 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. Output the results to standard output, in the following form: Sum = Mean = Standard Deviation =
#include <stdio.h> #include <math.h> #define NUM_DATA_POINTS 3 /* Fixed number of data points */ int main () { double x1, x2, x3; /* Input variables */ double sum; /* Computed sum */ double mean; /* Computed mean */ double std_dev; /* Computed standard deviation */ /* * Prompt the user for the input. */ printf("Enter three real numbers, separated by spaces: "); /* * Input the numbers. */ scanf("%lf%lf%lf", &x1, &x2, &x3); /* * Compute the sum. */ sum = x1 + x2 + x3; /* * Compute the mean. */ mean = sum / NUM_DATA_POINTS; /* * Compute the standard deviation. */ std_dev = sqrt((pow(x1 - mean, 2) + pow(x2 - mean, 2) + pow(x3 - mean, 2)) / NUM_DATA_POINTS); /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", mean); printf("Standard Deviation = %f\n\n", std_dev); return 0; }
gcc -ansi -pedantic -Wall -Werror stats.c a.out Enter three real numbers, separated by spaces: 1 2 3 Sum = 6.000000 Mean = 2.000000 Standard Deviation = 0.816497
std_dev = sqrt((pow(x1 - mean, 2) + pow(x2 - mean, 2) + pow(x3 - mean, 2)) / NUM_DATA_POINTS);to this
std_dev = sqrt((pow(x1 - mean, 2) + pow(x2 - mean, 2) + pow(x3 - mean, 2)) / (NUM_DATA_POINTS - 1 ));
/**** * * 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 * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 31mar11 * Last Modified: 4apr11 * */ #include <stdio.h> #include <math.h> #define NUM_DATA_POINTS 3 /* Fixed number of data points */ int main () { double x1, x2, x3; /* Input variables */ double sum; /* Computed sum */ double mean; /* Computed mean */ double std_dev; /* Computed standard deviation */ /* * Prompt the user for the input. */ printf("Enter three real numbers, separated by spaces: "); /* * Input the numbers. */ scanf("%lf%lf%lf", &x1, &x2, &x3); /* * Compute the sum. */ sum = x1 + x2 + x3; /* * Compute the mean. */ mean = sum / NUM_DATA_POINTS; /* * Compute the standard deviation. */ std_dev = sqrt((pow(x1 - mean, 2) + pow(x2 - mean, 2) + pow(x3 - mean, 2)) / (NUM_DATA_POINTS - 1 )); /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", mean); printf("Standard Deviation = %f\n\n", std_dev); return 0; }
gcc -ansi -pedantic -Wall -Werror stats.c a.out Enter three real numbers, separated by spaces: 1 2 3 Sum = 6.000000 Mean = 2.000000 Standard Deviation = 1.000000
/* * Return the sum of the given three numbers. */ double compute_sum(double x1, double x2, double x3) { return x1 + x2 + x3; }
/**** * * 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 * * * 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 */ double sum; /* Computed sum */ double mean; /* Computed mean */ double std_dev; /* Computed standard deviation */ /* * Prompt the user for the input. */ printf("Enter three real numbers, separated by spaces: "); /* * Input the numbers. */ scanf("%lf%lf%lf", &x1, &x2, &x3); /* * Compute the sum. */ sum = compute_sum(x1, x2, x3); /* * Compute the mean. */ mean = compute_mean(x1, x2, x3); /* * Compute the standard deviation. */ std_dev = compute_std_dev(x1, x2, x3); /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", mean); printf("Standard Deviation = %f\n\n", std_dev); 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 )); }
/**** * * 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 printed 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 * * * 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 (other vars gone) */ /* * Prompt the user for the input. */ printf("Enter three real numbers, separated by spaces: "); /* * Input the numbers. */ scanf("%lf%lf%lf", &x1, &x2, &x3); /* * Compute and output the results (preceding three assignment statements gone). */ 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 )); }
Input numbers must be non-negative. If a negative number is input, it is treated as a 0. The number of data points remains 3, even if one or more negative numbers is input. For example, with input 1 -2 3, the results are Sum = 4.000000 Mean = 1.333333 Standard Deviation = 1.527525
/**** * * 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 a 0. The number of data points remains 3, even if one or more * negative numbers is input. For example, with inputs * * 1 -2 3 * * The results are * * Sum = 4.000000 * Mean = 1.333333 * Standard Deviation = 1.527525 * * * 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 0. */ if (x1 < 0) x1 = 0; if (x2 < 0) x2 = 0; if (x3 < 0) x3 = 0; /* * 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 )); }
Input numbers must be non-negative. If a negative number is input, it is treated as 0. For each negative input, the number of data points is decremented by 1. For example, with inputs 1 -2 3, the results are Sum = 4.000000 Mean = 2.000000 Standard Deviation = 1.000000
/**** * * 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 0. For each negative input, the number of data points is * decremented by 1. For example, with inputs * * 1 -2 3 * * The results are * * Sum = 4.000000 * Mean = 2.000000 * Standard Deviation = 1.000000 * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 31mar11 * Last Modified: 3apr11 * */ #include <stdio.h> #include <math.h> /* * 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, int num_data_points); double compute_std_dev(double x1, double x2, double x3, int num_data_points); int main () { /* * Declare the variables used in main. */ double x1, x2, x3; /* Input variables */ int num_data_points; /* Number of data points */ /* * Initialize the number of data points to 3. */ num_data_points = 3; /* * 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 0, and drop it from stats by * decrementing the number of data points for each negative input. */ if (x1 < 0) { x1 = 0; num_data_points = num_data_points - 1; } if (x2 < 0) { x2 = 0; num_data_points = num_data_points - 1; } if (x3 < 0) { x3 = 0; num_data_points = num_data_points - 1; } /* * Compute and output the results. */ printf("Sum = %f\n", compute_sum(x1, x2, x3)); printf("Mean = %f\n", compute_mean(x1, x2, x3, num_data_points)); printf("Standard Deviation = %f\n\n", compute_std_dev(x1, x2, x3, num_data_points)); 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, and the given number * of data points. Question: What happens here if num_data_points = 0? */ double compute_mean(double x1, double x2, double x3, int num_data_points) { return compute_sum(x1, x2, x3) / num_data_points; } /* * Return the standard deviation of the given three numbers, and the given * number of data points. Question: What happens here if num_data_points = 0? */ double compute_std_dev(double x1, double x2, double x3, int num_data_points) { double mean = compute_mean(x1, x2, x3, num_data_points); return sqrt((pow(x1 - mean, 2) + pow(x2 - mean, 2) + pow(x3 - mean, 2)) / (num_data_points - 1 )); }
can be illustrated like this:int x; x = 10;