Find the harmonic mean

Hi! I'm having trouble with my code. We were tasked to find the harmonic mean.

Harmonic Mean = n / 1/x1 + 1/x2 ...

The n will be the user's input.
The x1, x2, ... is also the user's input but the number of x's is the same as n.

Thank you so much for helping!

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
  #include <iostream>
using namespace std;

double harmonic_mean(int arr[], int n);

int main(){
    int n,k;
    cin>>n;
    double arr[n];
    for(int i=0; i<n; i++){
        cin>>arr[i];
    }
    
    cout << harmonic_mean(arr,n);
    return 0;
}

double harmonic_mean(int arr[], int n){
    double harm_mean = 0,k=0, l;
    
    l = 1/arr;
    k = l++;
    harm_mean = n / k;
    return harm_mean;
 
}
L9 is not standard C++. It's accepted by some compilers as a non-standard language extension - but major compilers such as MS don't.

arr is defined as double in main() but is defined as int as the argument to harmonic_mean()! The types need to be the same.

Also, you need a loop in harmonic_mean() to iterate over the values. Consider:

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

double harmonic_mean(const double arr[], size_t n);

constexpr size_t maxn {50};

int main() {
	double arr[maxn] {};
	size_t n {};

	std::cout << "Enter number of values: ";
	std::cin >> n;

	std::cout << "Enter values: ";
	for (size_t i = 0; i < n && i < maxn; ++i)
		std::cin >> arr[i];

	std::cout << "harmonic mean is " << harmonic_mean(arr, n) << '\n';
}

double harmonic_mean(const double arr[], size_t n) {
	double harm_mean {};

	for (size_t e = 0; e < n; ++e)
		harm_mean += 1.0 / arr[e];

	return n / harm_mean;
}


Last edited on
Your harmonic mean function should use double arr[], not int arr[].

In your harmonic mean function, replace
1
2
    l = 1/arr;
    k = l++;

with a for loop (similar to that in your input) that successively adds 1.0/arr[i] to the sum of reciprocals, k.




But you could also do it straight from an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <valarray>
using namespace std;

int main(){
    int n;
    cout << "Enter n: ";   cin >> n;
    valarray<double> arr( n );
    
    cout << "Enter " << n << " values: ";
    for ( double &e : arr ) cin >> e;
    
    cout << "Harmonic mean is " << n / ( 1.0 / arr ).sum();
}
Enter n: 3
Enter 3 values: 2 3 4
Harmonic mean is 2.76923
Last edited on
The std::valarray is rather unique type in standard library; it has much more "math-oriented syntactic sugar".

std::vector is the "normal array" for modern C++. Resizable:
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
#include <iostream>
#include <vector>
using namespace std;

double harmonic_mean( const vector<double>& );

int main(){
    int n;
    cout << "Enter n: ";   cin >> n;
    vector<double> arr;
    arr.reserve( n );

    cout << "Enter " << n << " values: ";
    double e {};
    while ( arr.size() < n && cin >> e ) arr.emplace_back( e );
    
    cout << "Harmonic mean is " << harmonic_mean( arr ) << '\n';
}

double harmonic_mean( const vector<double>& values ) {
    if ( values.empty() ) return 0;
    double harm_mean {};
    for ( double e ; values ) harm_mean += 1.0 / e;
    return values.size() / harm_mean;
}
Topic archived. No new replies allowed.