Im supposed to create a program that will read 10 floating pt numbers into an array of 10 double. The program is supposed to quit on non numeric input, which i have succeeded in doing, and also the program is supposed to take an average of all of the numbers and report how many of the 10 numbers entered are above average (this is the part that im having trouble with). With the code shown below, the program takes an average but it doesnt report how many numbers are above average. Please help me.

#include <iostream>
#include <cctype>

int main()
{
          using namespace std;
          double input[10];
          double total = 0;
          double avg = 0;
          int count = 0;


          cout << "Enter the numbers (up to ten) then press enter, make sure each value is floating point\n";
          for (int i = 0; 10 > i; i++ )
          { 


                if (cin.fail() == false)
                {

                cin >> input[i];

                } 
                else
                {
                    break;
                    }

                total += input[i];    
          }

                avg = total / 10;
                cout << "Average is: " << avg <<endl;

                for ( int e = 0; input[e] > avg; e++)
                count++;


                cout << count << " Numbers are above average\n";
                cin.get();
                cin.get();
                cin.get();


          return 0;
}

Recommended Answers

All 2 Replies

Put your code in code tags ....

for ( int e = 0; input[e] > avg; e++)
    count++;

Like that ... it's easier to read. Also, that for loop is your problem.

Assume your input is 2 numbers: 0, 100 ... your average will be 50.00
input[0] = 0
avg = 50.00
The first comparison is input[0] > avg.

Your for loop comparison is:

for (int e = 0; 0 > 50; e++){
    // etc
}

You need to fix the logic in the for loop.

originally posted by kes166

Error is in your loop.
your loop should be like this

for (i=0; i<number_of_element;i++)
{
if(input[i]>avg
count++;
}

originally posted by kes166
for (int e = 0; 0 > 50; e++)

check your loop this loop not going to execute


Best Of Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.