//------------------------------------------------------------------- // areademo.cpp // // This program illustrates successive approximations to the // area under the graph y = x*x from x=0 to x=1 using // sums of areas of inscribed and circumscribed rectangles. // // compile using: $ g++ areademo.cpp -o areademo // execute using: $ ./areademo // // programmer: ALLAN CRUSE // written on: 19 APR 2006 //------------------------------------------------------------------- #include // for printf(), perror() #define N_MAX 100000 int main( int argc, char **argv ) { printf( "\n Area under the graph y = x*x from x=0 to x=1 \n\n" ); for (int n = 1; n < N_MAX; n+=n) { float deltaX = 1.0/n; float area_A = 0.0; float area_B = 0.0; for (int k = 1; k <= n; k++) { float x_L = ((float)(k-1))/n; float x_R = ((float)(k))/n; float y_B = x_L * x_L; float y_A = x_R * x_R; area_A += y_A * deltaX; area_B += y_B * deltaX; } printf( " n = %-8d ", n ); printf( "%1.5f < AREA < %1.5f \n", area_B, area_A ); } printf( "\n" ); }