Mean above mean

I need to make a program that reads in a list of floating-point numbers then calculates and displays the mean value for all values greater than the mean using arrays. It doesn't work, and I can't figure out why. I think it's something in
1
2
3
4
5
if(array1[condition] > mean) {
                       array2[condition] = array1[condition];          
                       number2++;
                                 
                       }

but I'm not sure. Here's my entire 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
#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 = 0, number2 = 0;
    float array1[100], array2[100];

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

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

    float var = Mean(array1, number1);

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

Can someone help me get this working?
Line 31. Lets say that the input is {1, 1, 10} and the mean thus 4.
There is only one value to copy, so you write value 10 into element array2[2]. Does that ring any bells?

Line 29. The input is now {10, 1, 1}.
The first iteration writes 10 into array2[0] and increments number2 to 1.
Second iteration resets the number2 back to 0.
Last iteration resets the number2 back to 0.
Thank you for the reply, but I'm still not sure what I should do. I fixed the number2 = 0 and now have:
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
#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 = 0, number2 = 0;
    float array1[100], array2[100];

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

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

    float var = Mean(array1, number1);
number2 = 0;
    for(int condition = 0; condition < number1; condition++) {
            
            if(array1[condition] > mean) {
                       array2[condition] = array1[condition];          
                       number2++;
                                 
                       }
                       }
    cout << Mean(array2, number2);
}

but it still doesn't work.
bump really need help
I really don't think you need 2 arrays. Try something like this maybe?

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
#include <iostream>
#include <iomanip>

using namespace std;

int Mean(float array[], float n, float v = 0.0) 
{
  int count = 0;
  float average = 0.0;
  for(int i = 0; i < n; ++i)
  {
    if(array[i] > v)
    {
      average += array[i];
      count++;
    }
  }
  return average/count;
}

int main()
{
  float m = 0.0;
  int n = 0;

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

  float a[n];

  for(int i = 0; i < n; ++i)
  {
    cout << "Enter array value " << i+1 << ": ";
    cin >> a[i];
    cout << endl;
  }

  m = Mean(a,n); // // with 3rd param defaulting to 0.0 will just do plain old mean

  cout << "Mean of array is : " << m << endl;
  cout << "Mean of array values above mean: " << Mean(a,n,m) << endl; // will do mean of values above m
}
[Warning] converting to `int' from `float'
return average/count;
Last edited on
but it still doesn't work.

Because the indexing of array2 on line 31 is still incorrect.
Last edited on
And what do I need to change to fix it?
You have to think.

What should be the index of the element of array2 that you will copy the first encountered "over mean" value to?

What should be the index of the element of array2 that you will copy the Nth encountered "over mean" value to?
The first index (0) for the first value, and so on for the Nth value....
But how would I implement that? array2[number1] = array1[???]? Not sure what would be the index of array1 there either.
Bump...........
I fixed that, but it still doesn't work.
7.52 is the mean above the mean. expected
What size array? 10
5.4
3.2
5.7
9
0
2.3
1.5
6
9
10

8.50 is the mean above the mean.
actual
bump
Topic archived. No new replies allowed.