//------------------------------------------------------------------- // geodist.cpp // // This program displays a probability distribution and graph // for the Geometric Distribution based on the parameter p . // // to compile: $ g++ geodist.cpp -o geodist // to execute: $ ./geodist // // programmer: ALLAN CRUSE // written on: 22 FEB 2011 //------------------------------------------------------------------- #include // for printf() #include // for sqrt() #define N_MAX 50 double p = 0.08; double mu, sigma, q; double freq( unsigned int k ) { // returns the value of the geometric distribution at k // (namely, q^(k-1) * p ) if ( k == 0 ) return 0.0; double retval = p; for (int i = 1; i < k; i++) retval *= q; return retval; } int main( int argc, char **argv ) { // initialize global variables int n = N_MAX; q = 1.0 - p; mu = 1.0 / p; sigma = sqrt( q / (p * p) ); // exhibit the geometric distribution function printf( "\n" ); printf( " Geometric 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 ); }