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;
}
``````

Edited by Dani: Formatting fixed

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by prvnkmr449

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.

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

You need to fix the logic in the for loop.

originally posted by kes166

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.