//------------------------------------------------------------------- // normdist.cpp // // This program displays a probability distribution and graph // for the Normal Distribution having parameters n and p . // // to compile: $ g++ normdist.cpp -o normdist // to execute: $ ./normdist // // programmer: ALLAN CRUSE // written on: 21 FEB 2011 //------------------------------------------------------------------- #include // for printf() #include // for sqrt(), exp() #define N_MAX 50 double p = 0.50; double mu, sigma; double freq( double x ) { // returns the value of the normal distribution at x double pi = 3.1415926589793; double sqroot_2pi = sqrt( 2.0 * pi ); double den = (sqroot_2pi * sigma); double arg = ( x - mu ) / sigma; double num = exp( - arg*arg / 2.0 ); return num / den; } int main( int argc, char **argv ) { // initialize global variables int n = N_MAX; mu = n * p; sigma = sqrt( mu * (1.0 - p) ); // exhibit the normal distribution function printf( "\n" ); printf( " Normal probability distribution" ); printf( " for n=%d and p=%1.5f \n\n", n, p ); for (int k = 0; k <= n; k++) { double prob = freq( (double)k ); printf( " k=%-3d ", k ); printf( "prob=%1.5f ", prob ); int m = (int)( 300.0 * prob ); for (int j = 0; j < m; j++) printf( "*" ); printf( "\n" ); } // report this distribution's statistical parameters printf( "\n n=%d mu=%1.5f sigma=%1.5f \n\n", n, mu, sigma ); }