//--------------------------------------------------------------------- // stdnorml.cpp // // This program displays a table showing areas under the standard // normal distribution from 0 to z > 0 in increments of 1/10. // // to compile: $ g++ stdnorml.cpp -o stdnorml // to execute: $ ./stdnorml // // programmer: ALLAN CRUSE // written on: 22 FEB 2011 //--------------------------------------------------------------------- #include // for printf() #include // for sqrt(), exp() #define PI 3.14159 double func( double x ) { // returns the value y = ( 1 / sqrt(2*pi) ) * (exp( - x*x / 2 ) double root_2pi = sqrt( 2.0 * PI ); double den = root_2pi * exp( x*x / 2.0 ); return 1.0 / den; } double area( double z ) { // returns the area from 0 to z > 0 under the graph of // the curve y = ( 1 / sqrt( 2*pi ) ) * exp( - x*x / 2 ). // computed using the trapezoid rule with 100 subintervals double delta_x = z / 100.0; double areasum = 0.0; double x0 = 0.0; double x1 = delta_x; for (int i = 0; i < 100; i++) { double y_value_L = func( x0 ); double y_value_R = func( x1 ); areasum += (y_value_L + y_value_R) * delta_x / 2.0; x0 += delta_x; x1 += delta_x; } return areasum; } int main( int argc, char **argv ) { // display the table's title and headline printf( "\n\n " ); printf( "Table of areas under the standard normal curve " ); printf( "from 0 to z \n\n" ); printf( "\n z " ); for (int i = 0; i < 10; i++) printf( " %d ", i ); // display the table's sideline and body double z = 0.0, delta_z = 0.10; for (int i = 0; i < 40; i++) { if ( (i % 10) == 0 ) printf( "\n %1.1f: ", z ); printf( " %1.3f ", area( z ) ); z += delta_z; } printf( "\n\n\n" ); }