Created
May 30, 2018 17:05
-
-
Save slwu89/b1aee32ef5bcd297696d3da0b5d2159b to your computer and use it in GitHub Desktop.
quick test that templated harmonic mean works with int and float
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CPP program to find harmonic mean of numbers. | |
#include <bits/stdc++.h> | |
using namespace std; | |
template <typename T> | |
float harmonicMean(T arr[], int n){ | |
// Declare sum variables and initialize | |
// with zero. | |
float sum = 0; | |
for (int i = 0; i < n; i++) | |
sum = sum + (float)1 / arr[i]; | |
return (float)n/sum; | |
} | |
int main() | |
{ | |
float arr_f[] = { 13., 14., 14., 15., 16. }; | |
int arr_i[] = { 13, 14, 14, 15, 16 }; | |
int n_f = sizeof(arr_f) / sizeof(arr_f[0]); | |
int n_i = sizeof(arr_i) / sizeof(arr_i[0]); | |
cout << harmonicMean(arr_f, n_f); | |
cout << "\n"; | |
cout << harmonicMean(arr_i, n_i); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment