/* Approximate the area under a curve using a Left Riemann Sum * Aaron Weeden, NCSI * June 28, 2011 */ #include /* for printf */ /* Constants */ #define X_LOWER 0.0 /* The lower value of the x-range under which to calculate the area */ #define X_UPPER 10.0 /* The upper value of the x-range */ #define WIDTH (X_UPPER - X_LOWER) /* The width of the x-range */ #define NUM_RECTANGLES 10 /* The number of rectangles to use in approximation */ #define RECT_WIDTH (WIDTH / NUM_RECTANGLES) /* The width of a rectangle */ float func(float x) /* the curve's function */ { return x * x; } int main() { /* Variables */ int i = 0; /* Loop index / rectangle ID */ float areas[NUM_RECTANGLES]; /* The area of each rectangle */ float sum = 0.0; /* The total approximated area */ /* Define the algorithm */ for(i = 0; i < NUM_RECTANGLES; i++) { areas[i] = RECT_WIDTH * func(i * RECT_WIDTH + X_LOWER); } /* Sum the areas */ for(i = 0; i < NUM_RECTANGLES; i++) { sum += areas[i]; } /* Print the sum */ printf("%f\n", sum); return 0; }