Skip to content

Commit

Permalink
Finished exercise 11.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblair committed Oct 18, 2010
1 parent f4cf3a9 commit 575ea83
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion ch1/exercises/ex11.c
@@ -1,9 +1,27 @@
/* I'm currently working on exercise 12, since it looks way harder. */
// Way to tell us about sqrt and pow.
// You'll need to invoke the compiler like so: clang -g -lm ex11.c
// I hope not all libraries need compiler flags...

#include <stdio.h>
#include <math.h>

#define RECTANGLES 100

int main(void) {

int i, radius = 2;
double height, area = 0, width = (float)radius / RECTANGLES;

double x = width / 2; // The first x value is the midpoint of the first rectangle.

for(i=0; i < RECTANGLES; i++) {
height = sqrt(pow(radius, 2) - pow(x, 2));
area += height * width;
x += width;
}

printf("The approximation of Π with %d rectangles is %.20f.\n", RECTANGLES, area);

return 0;

}

0 comments on commit 575ea83

Please sign in to comment.