#include #include #include #include #define TIMER_CLEAR (tv1.tv_sec = tv1.tv_usec = tv2.tv_sec = tv2.tv_usec = 0) #define TIMER_START gettimeofday(&tv1, (struct timezone*)0) #define TIMER_ELAPSED (double) (tv2.tv_usec-tv1.tv_usec)/1000000.0+(tv2.tv_sec-tv1.tv_sec) #define TIMER_STOP gettimeofday(&tv2, (struct timezone*)0) struct timeval tv1,tv2; double func(double num) { return sqrt(1-pow(num,2)); } int main(int argc, char * argv[]) { TIMER_CLEAR; if (argc < 2) { printf("Please specify the iterations\n"); return -1; } float iterations = atoi(argv[1]); // integral bounds float integral, sumOfRectangles; double a,b, area; a=0; b=1; double h = (b-a)/iterations; int i=0; TIMER_START; // sum up Trapezoids for (i; i <= iterations-1; i++) { area=h*func(a+(i-1)*h); sumOfRectangles+=area; } integral=4*sumOfRectangles; TIMER_STOP; //integral = h*((sqrt(1-pow(a,2))/2)+sumOfTrapezoids+sqrt(1-pow(a,2))/2); printf("Computed Sum for Rectangular Decomposition: %f\n", integral); printf("Time elapsed = %f seconds\n", TIMER_ELAPSED); }