Averaging code

This is a discussion on Averaging code within the C++ Programming forums, part of the General Programming Boards category; using accumulate and vectors is pretty neat also: Code: int num; vector<int> nums; while (cin >> num ) nums.push_back(num); double ...

  1. #16
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    using accumulate and vectors is pretty neat also:

    Code:
    int num;
    vector<int> nums;
    
    while (cin >> num ) nums.push_back(num);
    
    double avg = accumulate( nums.begin(), nums.end(), 0.0 ) / nums.size();

  2. #17
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by dra View Post
    using accumulate and vectors is pretty neat also:

    Code:
    int num;
    vector<int> nums;
    
    while (cin >> num ) nums.push_back(num);
    
    double avg = accumulate( nums.begin(), nums.end(), 0.0 ) / nums.size();
    I learned something new from this thread rofl. thanks!

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    using accumulate and vectors is pretty neat also:
    It is pretty much what Neo1 suggested, and just as wasteful if the only thing needed is to compute the arithmetic mean.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    If i needed to find the mean, I wouldn't have used that code...

    just something interesting to learn about.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21