The program illustrates the use of functions
/Programming Exercise #1 to Chapter 7 of “C++ Primer Plus. Sixth Edition” by Stephen Prata/
Write a program that repeatedly asks the user to enter pairs of numbers until at least one of the pair is 0.
For each pair, the program should use a function to calculate the harmonic mean of the numbers.
The function should return the answer to main(), which should report the result.
The harmonic mean of the numbers is the inverse of the average of the inverses and can be calculated as follows:
harmonic mean = 2.0 * x * y / (x + y)
#include
using namespace std;
double harmean(int x,int y);
int main()
{
int a, b;
cout<<"Please, enter two integers: ";
while (!(cin>>a)||!(cin>>b)||a==0||b==0)
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"\nPlease, enter two integers: ";
}
cout<<"\nThe harmonic mean of "<<a<<" and "<<b<<" = "<<harmean(a,b);
cout<<"\n\n";
return 0;
}
double harmean(int x, int y)
{
double hm=0.0;
hm=2.0*x*y/(x+y);
return hm;
}