Im trying to create a function that will calculate 2 input numbers.
When im typing for example 1.0 and 5.0 im getting 0.00 in the output..
You will probably find that the problem is something very obvious to you, but understand im a total newbie, and I just now getting to pointers.
This is my code:
#include <stdio.h>
#define MAXNUMS 50
double harmonic_mean(double number1, double number2);
int main(void)
{
double num1, num2;
printf("please enter two numbers to get the harmonic mean:\n");
while ((scanf("%lf%lf", &num1, &num2)) == 2)
{
printf("%.2lf", harmonic_mean(num1, num2));
}
return 0;
}
double harmonic_mean(double number1, double number2)
{
double harmonic, totalHarmonic;
int total;
int x, y;
double numbers[MAXNUMS];
for (x = 1, y = 0; x <= number2; x++, y++)
{
total = x;
numbers[y] = number1++;
}
for (x = 0; x <= total; x++)
{
harmonic += (1/numbers[x]);
}
totalHarmonic = total/harmonic;
return totalHarmonic;
}
tnx
Not sure what exactly you had in mind when you said harmonic mean, but the harmonic mean of two numbers is the reciprocal of the arithmetic mean of their reciprocals.
You don’t need to write for loops and other complicated code for it. The following code should do.