/* summation2.cpp is a driver program to test function Sum().
 *
 *   Input:   An integer n
 *   Output:  The sum of the integers from 1 through n
 *******************************************************************/

#include <iostream.h>       // cout, cin, <<, >>
#include <assert.h>         // assert

int Sum(int n);             // prototype for Sum()

int main()
{
  cout << "This program computes the sum of the integers from "
          "1 through n.\n";

  cout << "Enter a value for n: ";
  int n;
  cin >> n;

  cout << "--> 1 + ... + " << n << " = " << Sum(n) << endl;

  return 0;
}

/*** Insert the definition of function Sum()
     from Figure 6.1 here. ***/

/* Sum(n) computes the sum of the integers from 1 to n.
 * 
 * Receive:      n, an integer
 * Precondition: n > 0 
 * Return:       the sum 1 + 2 + ... + n
 *********************************************************/

int Sum (int n)
{
   assert(n > 0);

   int runningTotal = 0;

   for (int count = 1; count <= n; count++)
      runningTotal += count;

   return runningTotal;
}


