/* Include required header files */ #include #include /* Main program */ void main() { /* Declare & initialize variables */ double hgt = 0; /* Height of pyramid */ double base_length = 0; /* Base of pyramid */ double t = 0; /* Thickness of slice */ double lngth = 0; /* Length of slice */ int n_squares = 1; /* Number of squares */ double d_vol = 0; /* Volume of current slice */ double p_vol = 0; /* Volume total (after integration) */ int i = 0; /* Counter */ double j = 0; /* Temp */ /* Input hgt, base_length */ scanf("%lf %lf", &hgt, &base_length); /* Display Headers */ printf("\n\n\tNumber of Slices\tResultant Volume\tDifference\n"); /* Preform Integration */ while (!0) { j = p_vol; /* For purposes of ending loop, store old p_vol */ p_vol = 0; /* Reset p_vol */ n_squares *= 2; /* Double number of slices */ t = hgt / n_squares; /* Thickness of each slice */ /* Sum slices as per number of slices */ for (i = 1; i <= n_squares; i++) { lngth = (base_length / hgt)*(hgt - (i * t)); /* Length of slice */ d_vol = lngth * lngth * t; /* Volume of slice */ p_vol += d_vol; /* Add curent slice to total */ } /* End loop if difference of previous and current is < 0.001 */ if ((p_vol - j) > 0.001) { printf("\t%d\t\t\t%.7lf\t\t%.7lf\n", n_squares, p_vol, p_vol-j); } else { printf("\n\n\n"); exit(0); } } } /* End Program */