# Thread: Can't figure out why this program won't work

1. ## Can't figure out why this program won't work

This program is supposed to calculate the average, geometric mean, and harmonic mean of n number of numbers.

Code:
```#include <stdio.h>
#include <math.h>

int main()
{

double average, harmonic_mean, geometric_mean;
int num = 1;
int sum = 0;
int num_count = 0;
double invsum = 0.0;
int product = 1;

while (num!=0)
{
printf("Enter a number(0 to quit): ");
scanf("%d", &num);
if(num!=0)
{
sum = sum + num;

printf("The sum is: ");
printf("sum: %d\n", sum);
num_count = num_count + 1;
printf("num_count: %d\n", num_count);
product = product*num;
printf("product: %d\n", product);
invsum = invsum + (1/num);
printf("invsum: %f\n", invsum);

average = sum/num_count;
harmonic_mean = num_count/invsum;
geometric_mean = pow(product, 1.0/num_count);
}
}

printf("Average: %f\n",sum/(num_count));
printf("Harmonic mean : %f\n",num_count/(invsum));
printf("Geometric mean: %f\n", pow(product,1/(num_count)));

return(0);
}```
I did the printf of sum, num_count, product, and invsum to see why the average, harmonic mean, and geometric mean weren't coming out right, and it seems the problem comes from invsum, but I can't figure out why.

Could someone please help me? The average is obviously the sum of all the numbers divided by the number of numbers, and in case it is not clear what the harmonic mean and geometric mean are, here is what they are:

Harmonic: n/ [(1/x1) + (1/x2) + ... (1/xn)]
Geo: (x1 * x2 * ... *xn) ^ (1/n)

TIA for any help.

2. 1/num is an int
if num== 1 result is 1, if num is bigger - result is 0

use double arithmetics: 1.0 / num

3. type cast the number num

invsum = invsum + (1/(double)num);

and getch()

will work

4. I recompiled it with num as a doubles, and for some reason it always thinks the product is 1 now, anyone have any ideas? It also thinks sum and num are the same thing (sum = num, instead of equaling 5 if you entered 4 and 1, it says sum = 2).

It could just be my crappy compiler, could someone do a test run for me?

If you enter 3, 3, 2, average should be 2.67, harmonic should be 2.57, and geometric should be 2.62.

Thanks

Code:
```#include <stdio.h>
#include <math.h>

int main()
{

double average, harmonic_mean, geometric_mean;
double num = 1.0;
int sum = 0;
int num_count = 0;
double invsum = 0.0;
int product = 1;

while (num!=0)
{
printf("Enter a number(0 to quit): ");
scanf("&#37;d", &num);
if(num!=0)
{
sum = sum + num;

printf("The sum is: ");
printf("sum: %d\n", sum);
num_count = num_count + 1;
printf("num_count: %d\n", num_count);
product = product*num;
printf("product: %d\n", product);
invsum = invsum + (1/num);
printf("invsum: %f\n", invsum);

average = sum/num_count;
harmonic_mean = num_count/invsum;
geometric_mean = pow(product, 1.0/num_count);
}
}

printf("Average: %f\n",sum/(num_count));
printf("Harmonic mean : %f\n",num_count/(invsum));
printf("Geometric mean: %f\n", pow(product,1/(num_count)));

return(0);
}```

5. Perhaps you should read in your double with a double type of format specifier, then?

Code:
`scanf("%lf", &num);`
--
Mats

6. Ah, duh!

Ok, but now apparently there is another problem...

I entered 3, 3, 2. It said that the sum was 8 (correct), the product was 18 (correct), num count was 3 (correct), invsum was 1.167 (correct) and then I entered 0 to compute the others, and Harmonic was 2.57 which is correct, but the average was a HUGE number (about 40 numbers long), and the geometric mean was 1. Does this mean that num_count needs to be a double?

Still, I am not sure why the average does not work.

7. Yes! That was the problem! Thanks for all of your guys' help.