Thread: Averaging code

  1. #16
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    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.

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, 05:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04: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, 06:05 PM