Guest User

[C++11] Average (short)

a guest
Nov 14th, 2014
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4.  
  5. int main(){
  6.     int n = 0;
  7.     std::cout << "Please insert a sequence of numbers! (an invalid input stops the request)" << std::endl;
  8.     std::cout << "The average is: " << static_cast<double>(std::accumulate(std::istream_iterator<decltype(n)>(std::cin), std::istream_iterator<decltype(n)>(), 0, [&n](decltype(n) a, decltype(n) b){++n; return a + b;})) / std::max(n, 1) << std::endl;
  9.     return 0;
  10. }
Advertisement
Add Comment
Please, Sign In to add comment