Hello,

This is my first post. I am trying to teach myself C++ and am having a hard time with a function problem. This is not for homework, I simply want to learn something about C++ and I have no one else to ask.

The problem I am having is a simple function.
The problem in the book states:

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.0xXxY/(x+y)

Here is what I have gotten so far:

#include<iostream>
using namespace std;
void harmonic(int x, int y, float h);
int main()
{
    int x;
    int y;
    float h;
    
    
    while ( x != 0)
    {
          
    cout <<"Enter first integer: (or 0 to quit)";
    cin >> x;
    cout <<"Enter second integer: (or 0 to quit)";
    cin >> y;
    harmonic(x,y,h);
    

cout <<"The harmonic mean is: \n" << h <<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
          
void harmonic(int x, int y, float h)
{
h=(2*x*y)/x+y;
}

I think as a side note I should mention that I am using DEV-C++.

The output, when I enter x=2 and y=2, is 3.21412e-039. When I enter a "0", the program simply stops.

What have I done wrong?

Any help will be greatly appreciated.

>>The function should return the answer to main(),
That means that the function has a return type that is NOT void. The reason for the float typecasts below is to avoid the problems with integer division. With integer division all fractional parts are simply discarded, for example 1/2 is 0 but 1.0 / 2.0 is 0.5.

float harmonic(int x, int y)
{
    return (float)((2.0*x*y)) / (float)(x+y);
}

int main()
{
  ...
   h =  harmonic(x,y);
  ...
}

First thing is try a little harder to get the indentation consistent. Code is much easier to debug when it's consistently formatted.

Type double is prefered to type float in most cases.

If either x or y is zero, which is the same as both x and y not being zero, then the loop should stop. You check x, but not y.

Initialize x before you try to use it in the conditional of the while statement.

You need to pass h to harmonic by reference, not by value, or else use h as a return value instead of an argument to the function by declaring it locally to harmonic, and assigning it to the same type of variable back in main().

You may also need a loop in main checking if the harmonic is zero and if so then stop the program in addition to stopping the loop.

I would check also if x+y equals zero because you're not allowed to divide to 0.

First thing is try a little harder to get the indentation consistent. Code is much easier to debug when it's consistently formatted.

And to help you with this, here's some help

Hello again,

Thank you all for your help. I have one last question though. The While loop in the program leaves me wondering.

#include<iostream>
using namespace std;
float harmonic(int x, int y);
int main()
{
    int x;
    int y;
    float h;
    
    while ( x!=0 && y!=0)
    {
       cout <<"Enter first integer: (or 0 to quit)";
       cin >> x;
       cout <<"Enter second integer: (or 0 to quit)";
       cin >> y;
       h= harmonic(x,y);
       cout <<"The harmonic mean is: \n" << h <<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
          
float harmonic(int x, int y)
{
    return (float)((2.0*x*y)) / (float)(x+y);
}

The program does ok but when I want to exit I have to enter a "0" for both x and y for the program to quit. I have tried to use either && and || to get it to quit after just the first "0" is entered, Is there a better way to do this?

Many thanks

that loop still has the division by zero problem. You need to use another if statement

while( (x+y) != 0)
{
  ... <snip>
   if( (x+y) != 0)
   {
         h= harmonic(x,y);       
         cout <<"The harmonic mean is: \n" << h <<endl;
   }
}

Better formatting, thanks, but lines 18-20 should be indented to make it easier to determine that you've correctly closed the loop.

Then you need to initalize x and y outside the loop so the have a known value, not some random junk. Who knows, the random junk may cause the loop not to function the way you want.

Finally, to answer your question, the conditional looks like it should work. if x == 0 then x != 0 will be false and the AND will be false so the loop should stop. If x != 0, then the first part of the AND will be true, but if y == 0, then the second half of the AND will be false and the entire AND will be false. If x != 0 AND y != 0 then both parts of the AND will be true and the entire AND will be true so the loop should proceed. If you don't want to calculate the harmonic if either is zero then I'd insert an if into the body of the while statement to prevent it. Say between lines 15 and 16 add:

if(x != 0 && y != 0)

and put lines 16 and 17 inside the body of the if statement.

Now, if you've initialized x and y to some value other than zero the loop will start. If the user enters zero for either x or y then the harmonic will not be calculated and the loop will stop.

Alternatively you could put two if statements in. One when after x is entered and one after y. If either x or y is zero then put a break statement in and the loop should stop without calculating the harmonic. If you do it this way then the conditional of the while loop could be kept empty.

Thanks guys for all the help. Your suggestions worked awesome.

please give me a detailed explanation for your program

commented: 3 years too late -7
commented: I'll get you a time machine... -1

thak you for your help & give me another examples

thank for your on this c++ exercise.and if you another related exercise you have before show me

Maybe following will be useful for c++ learning. In procedure harmonic, you can pass argument h by reference. In this case, the code you have written in first post is correct excepting the argument list in the function. Correct version reads:

void harmonic(int x, int y, float &h)

After executing, function harmonic rewrites values of h at address &h. Here is more details:

http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc07cplr233.htm

So, I can use void type for return in this case. Function is calling using code:

harmonic(x,y,h);

That is what you did first. Passing arguments by reference saves memory if you use large arrays or matrices.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.