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

I was assigned an array with points. I am told to get the maximum value, the average value and inside the same array, if any point in the array is twice the average, I have to throw out the cout . So far, I got the average and maximum number in the array. but I can not install the program on cout outlier. Instead, it gives me a multiple of the average. here is the program;

 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; } 
+6
c ++
source share
5 answers

Your code contains a subtle and complex error. You use ary [i] after the final loop. At this point, the value of i is equal to max, so the if statement compares random memory, because you are leaving the end of the array.

Since this is C ++, not C, you could avoid this particular error by declaring your loop variables in a for loop, like this

 for (int i = 0; i < max; ++i) { .... } 
+10
source share

Here is a C ++ solution for your job, but you probably won’t be allowed to pass this :-)

 #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; } 
+4
source share

You will need to use two for loops. You have to go through ary and check each element for out , then cout << ary[i] .

This will probably be a little more obvious if you declare your variables where they are used in the smallest possible area.

For example:

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

and

 double outlier = avg * 2; 

By the way, it may be a little overhead (right now), but STL provides functions for determining max (max_element) and sum (accumulate) an array. It may be interesting to read.

0
source share

If it is exactly twice the average, it should be "==", and not more than twice as much. Why do you think maxv ? Try using more meaningful names.
You should not print ary[i] instead? Also, why don't you loop the array again with a for loop? Should you not go through all this to find all outliners, or should only the last element for the outliner be checked.

0
source share

I prepared the following program (mainly for my own training). He is trying to make the most of the standard C ++ library.

 #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 // (ie, 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; } 

My output (compiled with g++ (GCC 4.7.2) using the -std=c++11 flag).

 [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 
0
source share

All Articles