Writing a function that can modify it's number of parameters at runtime using vectors?

P

Protoman

I have a function that calculates the mean of the some numbers; I want
it to calculate the vector size at runtime. Can you help me out here?
Here's the function:

const long double& mean(long long x)
{
vector<int> v(x);
for(int i= 1; i <=x; ++i)
{
v.push_back(i);
}
const long double ret=accumulate(v.begin(), v.end(), 0.0) / v.size();
return ret;
}

If you could help me out, that'd be great. Thanks!!!
 
K

kevin.hall

This is a much faster equivalent to what you wrote:

const long double& mean(long long x)
{
double y = x;
return y*(y+1)/2;
}
 
P

Protoman

OK, now how do I let it accept user input for the data; I'm trying to
make averages for inputs other than 1 to x. And sorry for the mutiple
posting.
 
K

kevin.hall

You're the same programmer(!?!?!?) that needs an infinite-precision
floating point class for NASA! Come on... any programmer with a little
bit of experience would know (a) limitations of C/C++ and (b) how to
program something reasonalbe like what you requested in this thread (c)
look for books or free libraries to help them on these subjects. I
will not do your homework / school project for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top