提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我被分配设置一个带点的数组。 有人告诉我,以获得最大值,平均值,这相同的阵列中,如果阵列中的任何点是平均水平的两倍,我要cout
的“局外人”。 到目前为止,我已经获得了数组中的平均数和最大数。 但是我无法将程序设置为cout
异常值。 相反,它给了我平均值的倍数。 这是程序;
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;
}
您的代码包含一个微妙而棘手的错误。 你在最后的for循环之后使用ary [i]。 此时,i的值等于max,因此if语句比较随机内存,因为你要离开数组的末尾。
由于这是C ++而不是C,你可以通过像这样在for循环中声明循环变量来避免这个特定的错误
for (int i = 0; i < max; ++i) {
....
}
这是你的任务的C ++解决方案,但你可能不会被允许提交;-)
#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;
}
我准备了以下程序(主要是为了我自己的学习)。 它试图尽可能多地使用C ++标准库。
#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;
}
我的输出(使用-std=c++11
标志用g++
(GCC 4.7.2)编译)。
[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循环。 你应该穿越ary
和检查,对每一个元素out
,然后cout << ary[i]
如果您在尽可能小的范围内声明变量使用它们,这可能会更加明显。
例如:
for (int i = 0; ...) {
}
和
double outlier = avg * 2;
顺便说一句,这可能有点过头(现在),但STL提供了确定数组的max
(max_element)和sum
(accumulate)的函数。 这可能是有趣的阅读。
如果它恰好是平均值的两倍,那么它应该是'==',而不是大于平均值的两倍。
你为什么要输出maxv
? 尝试使用更有意义的名称。
你不应该打印ary[i]
吗? 另外,为什么不用for循环再次循环数组呢? 你不应该遍历它来找到所有的大纲,或者只应该检查最后一个元素的大纲。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.