/* This program approximates the definite integral of a function
     F(x) from x = a to x = b using the rectangle method.

   Input:   Endpoints a and b and the number n of rectangles
   Output:  Prompts to the user and the approximation of the integral

Note: To find the integral of a different F(x), redefine
      function F().
------------------------------------------------------------------*/

double F(double x)                  // the function being integrated
{
   return x*x + 1;                  // change here as desired...
}

//-----------------------------------------------------------------

#include <iostream.h>

#include "NumMethods.h"

int main(void)
{
   cout << "\nThis program approximates the definite integral of a"
           "\n\tfunction using the rectangle method.\n";

   int
      n;                               // the number of rectangles

   double
      a,                               // left endpoint of integral
      b,                               // right endpoint of integral
      ApproximateInt;                  // the approximate integral

   char
      Answer;                          // query response variable

   do
   {
      do                               // get the endpoints
      {
         cout << "\nEnter the endpoints (a & b) of the integral: ";
         cin >> a >> b;
      }
      while (a >= b);

      do                               // get the number of rectangles
      {
         cout << "Enter the number of rectangles to be used: ";
         cin >> n;

         ApproximateInt = RectangleMethod(F, a, b, n);

         cout << "--> An approximation to the integral using "
              << n << " rectangles is " << ApproximateInt << "\n\n";

         cout << "\nChange the number of rectangles (y or n)? ";
         cin >> Answer;
      }
      while ((Answer == 'y') || (Answer == 'Y'));

      cout << "\nChange the starting interval (y or n)? ";
      cin >> Answer;
   }
   while ((Answer == 'y') || (Answer == 'Y'));

   return 0;
}

