I'm having a problem getting the number of integers greater than the average in my array to display. I keep getting "You have 1 number greater than the average when there are 5 that are greater. Pls help!!!
This is the code I've devolped so far:

#include <iostream>


using namespace std;



int main(){

    const int SIZE = 10;
    int myArr[SIZE];
    double myAvg;
    int myCount = 0;
    cout<<"Give me 10 whole numbers: "<<endl;

    for(int i = 0; i<SIZE; i++){
        cin>>myArr[i];
    }

    myAvg = (myArr[0] + myArr[1] + myArr[2] + myArr[3] + myArr[4] + myArr[5] + myArr[6] + myArr[7] + myArr[8] + myArr[9]) / 10;
    if(myAvg < myArr[SIZE]){

    }

        myCount++;
        cout<<"You have "<<myCount<<" number(s) greater than the average"<<endl;



    system("pause");
    return 0;
}

Edited 4 Years Ago by deceptikon: Moved to C++ and fixed code tags

I think you should use a for statement to loop through your myArr array, comparing each element to the calculated average.

just sort your table and iterate over it to find index of the first greater and get the number from simple calculation: arraySize - firstGreaterIndex

You should do as gerard4143 says and use a loop to calculate your mean value. Even better, take than loop and put it in it's own function, something like:

double calculateMean( int* arr, int size );

You should also be careful since your method of calculating the mean doesn't do what you want it to. The mean of a bunch of int values can be a non-int value (the mean of the series {1,2}, for example). You need to cast the value of the sum of the elements of your array to a float or double value. You can use the static_cast operator to do this:

double mean = static_cast< double >( 1 + 2 ) / 2;

Finally, the reason that you keep getting the answer 1 is because the your counter variable is incremented outside the if statement on line 21. In fact, this statement is not correct either,since myArr[ SIZE ] isn't a valid element of myArr. You need another loop to go through all the elements of myArr and apply the check to see if they're greater than the mean.

This article has been dead for over six months. Start a new discussion instead.