tags:

views:

140

answers:

4

i have been assigned to set up an array with points. I am told to get the maximum value, average, nd within this same array, if any point in the array is twice the average, i should cout an "outlier." So far i have gotten the average and maximum numbers in the array. but i am unable to set the programme to cout the outlier. Instead it gives me a multiple of the average. pls help! here is the programme;

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;
}
A: 

You'll need to use two for-loops. You should be traversing the ary and checking each element against out, then cout << ary[i].

This would probably be a little more obvious if you declared your variables where they are used, in the smallest scope possible.

For example:

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

and

double outlier = avg * 2;

By the way, this may be a little over-your-head (right now), but STL provides functions for determining the max (max_element) and sum (accumulate) of an array. It might be interesting reading.

Stephen
I am assuming this is homework. Some teachers don't really like to see someone that use stuff they haven't thought. He also should learn how the algorithms are written. It's pretty basic.
the_drow
A: 

If it is exactly twice the average it should be '==' instead of bigger then twice the avarage.
Aslo why are you outputing maxv? Try to use more meaningful names.
Shouldn't you print ary[i] instead? Also, why aren't you looping the array again with a for loop? Shouldn't you iterate all over it to find all outliners or should only the last element be checked for an outliner.

the_drow
Technically you're correct, but I think the term "outlier" indicates that it should be "bigger than twice the average".
Stephen
+9  A: 

Your code contains a subtle and tricky to spot bug. You're using ary[i] after the final for loop. At this point, the value of i is equal to max, so your if statement is comparing random memory because you're going off the end of the array.

Since this is C++ and not C, you could have avoided this particular bug by declaring your loop variables in the for loop like this

for (int i = 0; i < max; ++i) {
    ....
}
Stewart
And it's a good habit: always declare your variables in the tighter scope possible.
Matthieu M.
+4  A: 

Here is a C++ solution to your assignment, but you probably won't be allowed to hand that in ;-)

#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;
}
FredOverflow
When you look at it, c++ is such an ugly language.
Stephen