#include #include // ======================================================================= // Settings for Integrations // ======================================================================= // The tntegrant function. double f(double x) { return cos(x); } double A = 0.0; // The lower-limit of the integration interval. double B = 1.0; // The upper-limit of the integration interval. int N = 10000; // The number of tiny intervals for numerical integration. // Be careful of the maximum limit of the "int" value in C programming language. // It depends on your environment, and it might be smaller than you thought. // Use "long long" or "int64_t" and so on, instead of "int", if necessary. // ======================================================================= // Compute The Integral Value Numerically // ======================================================================= int main() { double delta = (B - A) / N; // The width of a tiny interval. double value = 0.0; // Add approximated area of all tiny intervals to this variable. // Traverse tiny intervals from lower-limit (left) to upper-limit (right). for(int i=0; i