# Thread: Averaging the average of the numbers above the average program doesn't work

1. ## Averaging the average of the numbers above the average program doesn't work

I have a program that takes the average of entered numbers, and then averages only the numbers from the entered numbers over the average of all the numbers, and outputs that. For example:
Code:
```What size array? 5
5 6 7 8 9

8.50 is the mean above the mean.```
But my program doesn't work. Instead, I get:
Code:
```What size array? 5
5 6 7 8 9

35.00 is the mean above the mean.```
My code is:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;
float Mean(const float array, int n) {
float average;
for(int i = 0; i < n; ++i) {
average += array[i];

}
average /= n;
return average;
}
int main( ) {
float n;
float average;
float o;
float value = 0;
float array;
float newaverage = 0;
cout << "What size array? ";
cin >> o;
n = o;
for(int i = 0; i < n; ++i) {
//   newarray = array[i];
cin >> array[i];
newaverage = average;
if(newaverage < average) {
newaverage = average;
}
average += array[i];

}
newaverage /= n;
cout << fixed << setprecision(2) << average << " is the mean above the mean.";
}``` 2. Update: I worked on it a bit and I've made some progress but it still doesn't work.
Can someone help? My current code:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;
float Mean(float array[], float n) {
float average;
for(int i = 0; i < n; ++i) {
average += array[i];

}
average /= n;
return average;
}
int main()
{
int mean;
int number1, number2;
float array1, array2;

cout << "What size array? ";
cin >> number1;

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

Mean(array1, number1);

for(int condition = 0; condition < number1; condition++) {
number2 = 0;
if(array1[condition] > mean) {
array2[number2] = number2;
number2++;

}
}

cout << Mean(array2, number2);
}``` 3. You must set the assignment of 'number2' outside the loop, or else it will be reset on every iteration. 4. Originally Posted by Alpo You must set the assignment of 'number2' outside the loop, or else it will be reset on every iteration.
Okay, now it gives me 3.4 (still wrong)
Current code:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;
float Mean(float array[], float n) {
float average;
for(int i = 0; i < n; ++i) {
average += array[i];

}
average /= n;
return average;
}
int main()
{
int mean;
int number1, number2;
float array1, array2;

cout << "What size array? ";
cin >> number1;

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

Mean(array1, number1);

number2 = 0;
for(int condition = 0; condition < number1; condition++) {
//    number2 = 0;
if(array1[condition] > mean) {
array2[number2] = number2;
number2++;

}
}

cout << Mean(array2, number2);
}``` 5. Admittedly I am still unclear on your description of the problem. I do not understand what the "mean above the mean" is yet, so I cannot help you with the math.

This may be precluding others from helping as well. 6. Originally Posted by whiteflags Admittedly I am still unclear on your description of the problem. I do not understand what the "mean above the mean" is yet, so I cannot help you with the math.

This may be precluding others from helping as well.
It's displaying the mean of all the values entered greater than the mean. 7. You aren't initializing "average" on line 5.
You aren't assigning array1[condition] to array2[number2] on line 37.
You haven't assigned 'mean' on line 30.

Why is 'mean' an int?
Why is 'n' a float? 8. Your Mean function needs correcting. Set average to 0 before you start adding.

Also, array2 does not actually contain numbers from array1. You assigned elements as follows:
Code:
```            if(array1[condition] > mean) {
array2[number2] = number2;
number2++;

}```
number2 has nothing to do with array1's elements, it stands for where you are in array2.

edit: must be faster! 9. Originally Posted by Alpo You aren't initializing "average" on line 5.
You aren't assigning array1[condition] to array2[number2] on line 37.
You haven't assigned 'mean' on line 30.

Why is 'mean' an int?
Why is 'n' a float?
Average fixed.
Line 37 assignment fixed.
Mean assignment fixed.
As for the last two, does it make a difference?
Current code:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;
float Mean(float array[], float n) {
float average = 0;
for(int i = 0; i < n; ++i) {
average += array[i];

}
average /= n;
return average;
}
int main()
{
int mean = 0;
int number1, number2;
float array1, array2;

cout << "What size array? ";
cin >> number1;

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

Mean(array1, number1);

number2 = 0;
for(int condition = 0; condition < number1; condition++) {
//    number2 = 0;
if(array1[condition] > mean) {
array2[number2] = number2;
number2++;
array1[condition] = array2[number2];
}
}
cout << Mean(array2, number2);
}``` 10. Originally Posted by Kelton2 As for the last two, does it make a difference?
'n' being a float is probably less of a problem than 'mean' being an int. If your mean function returns a float, you don't won't it truncated, or the results could be wonky.

As for the rest, here look:

Code:
`    Mean(array1, number1); // Since you are using the variable 'mean' later, I assumed this would assign it a value?`
Code:
```if(array1[condition] > mean) {
array2[number2] = number2;
number2++;
// array1[condition] is a value larger that 'mean' (see above).
// You need to assign array2[number2] the value array1[condition]
// The incrementing of 'number2' should come after, so you don't have junk in array2
array1[condition] = array2[number2];
}
``` 11. Originally Posted by Alpo 'n' being a float is probably less of a problem than 'mean' being an int. If your mean function returns a float, you don't won't it truncated, or the results could be wonky.

As for the rest, here look:

Code:
`    Mean(array1, number1); // Since you are using the variable 'mean' later, I assumed this would assign it a value?`
Code:
```if(array1[condition] > mean) {
array2[number2] = number2;
number2++;
// array1[condition] is a value larger that 'mean' (see above).
// You need to assign array2[number2] the value array1[condition]
// The incrementing of 'number2' should come after, so you don't have junk in array2
array1[condition] = array2[number2];
}
```

Still not working:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;
float Mean(float array[], float n) {
float average = 0;
for(int i = 0; i < n; ++i) {
average += array[i];

}
average /= n;
return average;
}
int main()
{
float mean = 0;
int number1, number2;
float array1, array2;

cout << "What size array? ";
cin >> number1;

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

mean = Mean(array1, number1);

number2 = 0;
for(int condition = 0; condition < number1; condition++) {
//    number2 = 0;
if(array1[condition] > mean) {
array2[number2] = array1[condition];

array1[condition] = array2[number2];
}
number2++;
}
cout << Mean(array2, number2);
}``` 12. Number2 is not necessarily the length of array2. This is because number2 always increases, even if the if comparing the mean to some element is false.
You will need to move that statement.

Also really why are you not indenting? Do you actually enjoy reading code like that? The braces can't help you this way. 13. Originally Posted by whiteflags Number2 is not necessarily the length of array2. This is because number2 always increases, even if the if comparing the mean to some element is false.
You will need to move that statement.

Also really why are you not indenting? Do you actually enjoy reading code like that? The braces can't help you this way.
Where should I move the statement?
And if the indenting is really giving people that much of a problem, I will start indenting. 14. Well I would appreciate it if you indented.

Where should I move the statement?
For once think and find it out yourself. This is a working alternative with a big hint: You can always do this instead: array1[condition] = array2[number2++]; since you will only need number2 increased after that assignment. 15. Originally Posted by whiteflags Well I would appreciate it if you indented.

You can always do this instead: array1[condition] = array2[number2++]; since you will only need number2 increased after that assignment.
Where do I need to put that? In place of
array2[number2] = array1[condition];? I tried putting it in place of array1[condition] = array2[number2]; and it didn't work. Popular pages Recent additions 