In your write-up include (a) statement of the problem, (b) a picture which illustrates what is going on, (c) the text of the C program, (d) its output for n = 10, 100, 1000, where n is the number of subdivisions, per the discussion in class. Use your result to give approximate values of pi. Contrast and compare these with your previous method for computing pi. Finally, discuss the precision of your results.
PSEUDOPROGRAM: Given: n, the number of subdivisions and f, a function Compute: the area under the graph of f between a and b A <- 0 # initialize the area to zero a <- 0 # left end point b <- 1 # right end point x <- a # starting value of x dx <- (b-a)/n # the size of the subintervals for i from 1 to n # the loop (heart of this program) A <- A + f(x)dx # accumulate areas of strips in A x <- x + dx # advance x
% gcc -o area area.c -lmThe option -lm stands for ``load math'' library. Below is an example of a simple (and silly) C program which uses <math.lib> and which must be compiled with the -lm option.
#include <stdio.h> #include <math.h> main() { float a = 2.0; float b = sqrt(a); printf(" result = %f\n", b ); b = exp(a); printf(" result = %f\n", b ); b = log(a); printf(" result = %f\n", b ); b = exp(log(a)); printf(" result = %f\n", b ); }