使用向量计算算术平均值的函数? [英] Function that calculates the arithmetic mean using vectors?

查看:556
本文介绍了使用向量计算算术平均值的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算某些数字的平均值的函数;我需要

它接受任意数量的参数(一个需要是

其他参数的数量)你能帮助我吗?这里的功能是:


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();

返回ret;

}


我希望它在运行时计算矢量大小。如果你能帮助我

,那就太好了。谢谢!!!

I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) 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;
}

I want it to calculate the vector size at runtime. If you could help me
out, that''d be great. Thanks!!!

推荐答案

Protoman写道:
Protoman wrote:
我有一个计算平均值的函数一些数字;我需要它接受任意数量的参数(一个需要是其他参数的数量)你能帮助我吗?


你需要看到''fprintf''的实现。 va_args和va_list

是要使用的宏。在网上搜索如何使用
使用它们的例子。

[..]
I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) Can you help me out here?
You need to see an implementation of ''fprintf''. va_args and va_list
are the macros to be used. Search the web for an example of how to
use them.
[..]




V



V


我想使用矢量。

I want to use vectors though.


Protoman写道:
Protoman wrote:
我有一个函数来计算某些数字的平均值;我需要它接受任意数量的参数(一个需要是其他参数的数量)你能帮助我吗? [..]
I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) Can you help me out here? [..]




''va_start'',''va_end''和''va_list''是您应该阅读的宏

左右。网络是你的朋友。



''va_start'', ''va_end'', and ''va_list'' are the macros you should read
about. The web is your friend.


这篇关于使用向量计算算术平均值的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆