fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <functional>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<double> v {1.2, 6.0, 66.3, 9.1, 5.5, -0.5, 2.2, 7.8, 2.2};
  10. auto first_negative = begin(v);
  11. while (*first_negative > 0.0 && first_negative != end(v))
  12. ++first_negative;
  13. double geometric_mean =
  14. pow(accumulate(begin(v), first_negative, 1.0, multiplies<double>()),
  15. 1.0 / static_cast<double>( distance( begin(v), first_negative ) ));
  16. cout << geometric_mean;
  17. return 0;
  18. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
7.51017