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