Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » BeginnersRSS Feeds

The program illustrates the use of functions

Posted By: Peter Veropotvelyan     Category: C++ Programming     Views: 14696

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;
}
  
Share: 


Didn't find what you were looking for? Find more on The program illustrates the use of functions Or get search suggestion and latest updates.

Peter Veropotvelyan
Peter Veropotvelyan author of The program illustrates the use of functions is from Ukraine.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!