in_prod.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. //  If you use Visual C++, set the compile options to /GX
  2. #ifdef __BCPLUSPLUS__
  3. #include <iostream.h>
  4. #include <vector.h>
  5. #include <numeric.h>
  6. #include <iterator.h>
  7. #else
  8. #include <iostream>
  9. #include <vector>
  10. #include <numeric>
  11. #include <iterator>
  12. #endif
  13. using namespace std;
  14. typedef vector<float> FloatArray;
  15. typedef ostream_iterator<float, char, char_traits<char>>
  16. FloatOstreamIt;
  17. void main(void)
  18. {
  19. FloatOstreamIt itOstream(count, " ");
  20. //Initialize the arrays
  21. FloatArray rgF1, rgF2;
  22. for (int i=1; i<=5; i++)
  23. {
  24. rgF1.push_back(i);
  25. rgF2.push_back(i*i);
  26. };
  27. //print the arrays
  28. cout << "Array 1: ";
  29. copy(rgF1.begin(), rgF1.end(), itOstream);
  30. cout << endl;
  31. cout << "Array 2: ";
  32. copy(rgF2.begin(), rgF2.end(), itOstream);
  33. cout << endl;
  34. //This is the sum of the products (S.O.P) of the corresponding elements
  35. float ip1 = inner_product(rgF1.begin(), rgF1.end(), rgF2.begin(), 0);
  36. cout << "The inner product (S.O.P.) of Array and Array2 is " << ip1 << endl;
  37. //This is the product of the sums (P.O.S.) of the corresponding elements
  38. float ip2 = inner_product(rgF1.begin(), rgF1.end(), rgF2.begin(), 1,
  39. multiplies<float>(), plus<float>());
  40. cout << "The inner product (P.O.S.) of Array2 is " << ip2 << endl;
  41. }