

/* TrapezoidalArea computes the approximate area under a curve for
 * which a collection of y values at equally spaced x values are
 * entered from the keyboard.
 *
 *   Receive:      n, number of subintervals along the x axis
 *                 intLength, the length of the interval on the x axis
 *   Precondition: the y values correspond to equally-spaced x values 
 *                 in the interval on the x axis.
 *   Return:       the approximate area under the curve
 ************************************************************************/

double TrapezoidalArea(int n, double intLength)
{
   double yValue;
   cout << "First y value? ";
   cin >> yValue;                         // the first y value
   double sum = yValue / 2.0;             // initialize sum to 1/2 of it

   for (int i = 1; i <= n - 1; i++) 
   {
      cout >> "Next y value?  ";;
      cin >> yValue;                      // i-th y value
      sum += yValue;                      // add it to sum
   }

   cout << "Last y value?  ";
   cin >> yValue;                         // the last y value
   sum += yValue/ 2.0;                    // add 1/2 of it to sum

   double deltaX = intLength / double(n - 1);

   return deltaX * sum;                   // total area of trapezoids
}


