Calculating average problem

Hello, i was required to make a program that will prompt the user to input 10 numbers and put it into an array and then calculate the average. It also needs to report how many numbers in the array that are larger than the average.

Everything is successful except the latter part. I don't know what is wrong with my for loop statement that will determine how many members of the array are larger than the average. I tried everything, all i get is 0 and 10.

Here is my int main () code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
         const int donation = 10;

        cout << endl << endl;
        cout << "Instructions: Please input 10 numeric number, the program\n"
             << "will calculate its average and determine what number is higher\n"
             << "than the average.\n\n";

        double* donationArr = new double [donation]; // Dynamic array of 10 double
        int arrayCount = 0;
        cout << "Input number #1: ";

        while (arrayCount < donation && cin >> donationArr[arrayCount]) // Continue looping if the input didnt fail and the arrayCount is less than the donation.
        {                                                               // Also this will put number in the arrayCount.
            if (++arrayCount < donation) // execute the statement below if true
                cout << "Input number #" << arrayCount+1 << ": ";
        }

        double averageAns = 0.0;
        int arrayHigher = 0;

        // Calculate the number inside the array
        for (int counter = 0; counter < donation; ++counter)
            averageAns += donationArr[counter];
        // Count the number that is higher than the average
        for (int counter = 0; counter < donation; ++counter)
            if (averageAns < donationArr[counter])
                ++arrayHigher;


        // Show result

        if (arrayCount == 0)
            cout << "\n\nNo input! The program is terminated.\a\a\n\n";
        else
        {
            cout << "\n\nTotal: " << averageAns << endl;
            cout << "\n\nAverage: " << averageAns / donation << endl;
            cout << "Number higher than average = " <<arrayHigher << endl << endl;
        }




        delete [] donationArr; // Freeing the dynamic array of 10 double
        return 0;
    }
Last edited on
At the time you enter the loop on line 25, averageAns contains the sum of the array, not the average.
Ooh, such simple answer. Why i didn't think of that? Haha

Thank you very much for answering my question. :)
Topic archived. No new replies allowed.