/* This program tests function NextSumTerm().

   Output: The summation from 1 through i, for all i from 1 to 100
------------------------------------------------------------------*/

#include <iostream.h>

int NextSumTerm(void);

int main(void)
{
   for (int i = 1; i <= 100; i++)
      cout << i << ": " << NextSumTerm() << endl;

   return 0;
}

/* This function returns the sum of the integers from 1 to n, where n
    number of times the function has been called.

   Precondition: The function has been called for the n-th time
   Return:       The summation from 1 to n
--------------------------------------------------------------------*/

int NextSumTerm(void)
{
   static int
      CallNumber = 0;

   CallNumber++;

   return CallNumber * (CallNumber + 1) / 2;  // Gauss' formula
}                                             //   see Section 6.9

