[英]Trying to get a number within an array that is twice the average
我被分配設置一個帶點的數組。 有人告訴我,以獲得最大值,平均值,這相同的陣列中,如果陣列中的任何點是平均水平的兩倍,我要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.