Trying to get a number within an array that is twice the average

``````int main()
{
const int max = 10;
int ary[max]={4, 32, 9, 7, 14, 12,  13, 17, 19, 18};
int i,maxv;
double out,sum=0;
double av;

maxv= ary[0];

for(i=0; i<max; i++)
{
if(maxv<ary[i])
maxv= ary[i];

}
cout<<"maximum value: "<<maxv<<endl;

for(i=0; i<max; i++)
{

sum = sum + ary[i];
av = sum / max;
}
cout<<"average: "<<av<<endl;

out = av * 2;

if(ary[i]>out)
{
cout<<"outlier:  "<<maxv<<endl;
}
else
{
cout<<"ok"<<endl;
}

return 0;
}
``````
5 个回复

``````for (int i = 0; i < max; ++i) {
....
}
``````

``````#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>

int main()
{
const int N = 10;
int ary[N] = {4, 32, 9, 7, 14, 12, 13, 17, 19, 18};

int max = *std::max_element(ary, ary + N);
std::cout << "maximum: " << max << std::endl;

double average = std::accumulate(ary, ary + N, 0.0) / N;
std::cout << "average: " << average << std::endl;

std::cout << "outlier: ";
std::remove_copy_if(ary, ary + N,
std::ostream_iterator<int>(std::cout, " "),
std::bind2nd(std::less_equal<double>(), 2 * average));
std::cout << std::endl;
}
``````

``````#include<iostream>
#include<iterator>
#include<vector>
#include<algorithm>

int main() {
std::vector<float> nums;
// this will read the numbers from standard input; it will continue
// for as long as it can read floats (to stop you can enter a
// letter, or press Ctrl+D)
std::copy(std::istream_iterator<float>(std::cin),
std::istream_iterator<float>(),
std::back_insert_iterator<std::vector<float>>(nums));

// calculate the mean
float mean = std::accumulate(nums.begin(), nums.end(), 0) / nums.size();

std::cout<<"Mean of "<<nums.size()<<" numbers: "<<mean<<std::endl;

// create a lambda function which returns true if a number is BELOW
// twice the mean
auto fun = [&mean](float x) {return x < 2.0 * mean;};

// partition the list of numbers: those for which the lambda is true
// (i.e., the ones BELOW twice the man) will come before the
// outliers; the stable sort ensures that within each partition the
// numbers come in the original order
auto mark = std::stable_partition(nums.begin(), nums.end(), fun);

// mark gives an iterator to the first element of the second
// partition; it it is before the end we report the outliers
if(mark!=nums.end()) {

std::cout<<"Found "<<nums.end()-mark<<" outliers:"<<std::endl;

for(auto it=mark; it!=nums.end(); ++it) {
std::cout<<"\t"<<*it<<std::endl;
}

} else {
std::cout<<"No outliers found."<<std::endl;
}

return 0;
}
``````

``````[Prompt] ./a.out
1 2 3 4 5 20 f # the f is to end the stream of numbers; press enter
Mean of 6 numbers: 5
Found 1 outliers:
20
``````

``````for (int i = 0; ...) {
}
``````

``````double outlier = avg * 2;
``````

1 计算两个平均值的两倍？

2015-01-14 06:22:49 4 1146   java
4 从数字中获得两倍

2013-08-16 14:52:54 3 84   lua
5 试图计算并打印平均值并将其返回为 而不是一个数字？ 蟒蛇

2013-04-02 13:45:36 3 95   python
7 两个数字的安全平均值的解释

9 如何找到环绕的两个数字的平均值？

10 以相同的精度计算两个数字的平均值

2013-01-30 16:05:50 5 4159   average