Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:19:49+00:00 2026-05-26T09:19:49+00:00

It must be the most common function for what everyone has a code snippet

  • 0

It must be the most common function for what everyone has a code snippet somewhere, but I have actually spent no less than 1.5 hour searching for it on SO as well as on other C++ sites and have not found a solution.

I would like to calculate the mean of a double array[] using a function. I would like to pass the array to the function as a reference. There are millions of examples where the mean is calculated in a main() loop, but what I am looking for is a function what I can put in an external file and use it any time later.

So far, here is my latest version, what gives a compile error:

double mean_array( double array[] )
{
    int count = sizeof( array ) / sizeof( array[0] );
    double sum = accumulate( array, array + count, 0 );
    return ( double ) sum / count;
}

The compile error is:

error C3861: ‘accumulate’: identifier not found

Can you tell me how to fix this function? What does that compile error mean?

If I use std::accumulate (over the already defined using namespace std), then I get the following error:

'accumulate' : is not a member of 'std'
'accumulate': identifier not found

Why is ‘accumulate’ not a member of ‘std’?

p.s.: I know I can do ‘sum += array[i]’ way and not use accumulate, but I would like to understand what is happening here and how can I make my example work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:19:49+00:00Added an answer on May 26, 2026 at 9:19 am

    Try to add

    #include <numeric>
    

    It will bring in the ‘std::accumulate’ function you’re looking for.

    Going further, you’re gonna have a problem to find out the number of elements in your array. Indeed, an array cannot be passed to a function in the hope that the function will be able to know the size of the array. It will decay to a pointer. Therefore, your count calculation will be wrong. If you want to be able to pass an actual size specified array, you have to use a templated function.

    template <int N>
    double mean_array( double ( & array )[N] )
    {
        return std::accumulate( array, array + N, 0.0) / (double)(N);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two controllers which share most of their code (but must be, nonetheless,
I have a program that still must target Windows XP (_WIN32_WINNT 0x501), as most
I have this code to get the caller of my function's file name, line
This seems like the most common relationship but for some reason I cannot get
I have read a few tutorials and did some search but I must be
Most of javascript and web development books/articles says that you must put CSS in
I must be getting daft, but I can't seem to find how to read
What are the most common reasons for an outlet (a class property) not being
I have a few different applications that share some common DLLs. At the moment
I have a class ( MyClass ) that inherits most of its functionality from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.