home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / PARTSUM.CPP < prev    next >
Text File  |  1997-02-14  |  1KB  |  41 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.    // Initialize a vector using an array of integers.
  11.    //
  12.    int d1[10] = {1,2,3,4,5,6,7,8,9,10};
  13.    vector<int> v(d1+0, d1+10);
  14.    //
  15.    // Create an empty vectors to store results.
  16.    //
  17.    vector<int> sums((size_t)10), prods((size_t)10);
  18.    //
  19.    // Compute partial_sums and partial_products.
  20.    //
  21.    partial_sum(v.begin(), v.end(), sums.begin());
  22.    partial_sum(v.begin(), v.end(), prods.begin(), times<int>());
  23.    //
  24.    // Output the results.
  25.    //
  26.    cout << "For the series: " << endl << "     ";
  27.    copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
  28.    cout << endl << endl;
  29.  
  30.    cout << "The partial sums: " << endl << "     " ;
  31.    copy(sums.begin(),sums.end(), ostream_iterator<int>(cout," "));
  32.    cout <<" should each equal (N*N + N)/2" << endl << endl;
  33.  
  34.    cout << "The partial products: " << endl << "     ";
  35.    copy(prods.begin(),prods.end(), ostream_iterator<int>(cout," "));
  36.    cout << " should each equal N!" << endl;
  37.  
  38.    return 0;
  39.  }
  40.  
  41.