home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / ACCUM.CPP < prev    next >
Text File  |  1997-02-14  |  887b  |  36 lines

  1.  #include <numeric>    // For accumulate.
  2.  #include <vector>     // For vector.
  3.  #include <functional> // For times.
  4.  
  5.  using namespace std;
  6.  
  7.  int main ()
  8.  {
  9.    //
  10.    // Typedef for vector iterators.
  11.    //
  12.    typedef vector<int>::iterator iterator;
  13.    //
  14.    // Initialize a vector using an array of integers.
  15.    //
  16.    int d1[10] = {1,2,3,4,5,6,7,8,9,10};
  17.    vector<int> v1(d1+0, d1+10);
  18.    //
  19.    // Accumulate sums and products.
  20.    //
  21.    int sum  = accumulate(v1.begin(), v1.end(), 0);
  22.    int prod = accumulate(v1.begin(), v1.end(), 1, times<int>());
  23.    //
  24.    // Output the results.
  25.    //
  26.    cout << "For the series: ";
  27.    for(iterator i = v1.begin(); i != v1.end(); i++)
  28.       cout << *i << " ";
  29.  
  30.    cout << " where N = 10." << endl;
  31.    cout << "The sum = (N*N + N)/2 = " << sum << endl;
  32.    cout << "The product = N! = " << prod << endl;
  33.  
  34.    return 0;
  35.  }
  36.