/* ------------------------------------------------------------------- Unmodular approach to solving a problem: Implements Rectangle Rule completely in the MAIN function ------------------------------------------------------------------- */ #include float f(float x) { return(x*x); } int main(int argc, char *argv[]) { extern float f(float); float a, b, h, x, integral; int N, i; cout << "Enter start of interval: "; cin >> a; cout << "Enter end of interval: "; cin >> b; cout << "Enter N (number of segments): "; cin >> N; // ----------------------------------------------------------- // This section implements the Rectangle rule // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv integral = 0.0; h = (b-a)/N; x = a; for (i = 1; i <= N; i = i + 1) { integral = integral + f(x)*h; x = x + h; } // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // This section implements the Rectangle rule // ----------------------------------------------------------- cout << "Approximate finite integral = " << integral << "\n"; }