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.