# Program for average, geometric mean, harmonic mean

This is a discussion on Program for average, geometric mean, harmonic mean within the C Programming forums, part of the General Programming Boards category; I want to write a program that reads a series of numbers and calculates the average, geometric mean, and harmonic ...

1. ## Program for average, geometric mean, harmonic mean

I want to write a program that reads a series of numbers and calculates the average, geometric mean, and harmonic mean. I know the jist of what to do, but I don't know how to write the program.

Here is what I have come up with for the program:
The average is: sum/n
The harmonic mean is: n/inverse sum (ex: 4/((1/5)+(1/3)+(1/2)+(1/4)))
The geometric mean is: (X1 * X2 * X3 * Xn)^(1/n) - to write this in C I know it is pow (product, 1.0/n);

If given a set of numbers such as:
Number 1 = 5... then the sum is 5, product is 5, and inverse sum is 1/5
Number 2 = 3... then the sum is 8, product 15, inv sum 1/5 + 1/3
and so on...

I have an idea of what to do but I'm not sure how to do it properly. I think it is something like below:
Code:
```int num = 1;
while (num!=0)
{
printf("Enter a number(0 to quit): ")
scanf("&#37;d", &num);
if (num!= 0)
{ Enter numers
}
} (enter zero)
do the execution of the equations```
If anyone could help me with the parts I am missing I would appreciate it.

2. You could store the numbers in a linked list, or an array (assuming you want to keep all the numbers).

Such as,
Code:
```#define MAX_EQUATIONS 32

/* ... */

int num = 0,
i = 0,
z = 0,
product = 0,
m = 0;
int numbers[MAX_EQUATIONS] = {0};

/* ... */

for(m = 0; m < MAX_EQUATIONS; m++)
{
printf("Enter a number (0 to quit): ");
scanf("&#37;d", &num);

if(num != 0)
{
numbers[i] = num;
++i;
}else{
break;
}
}

/* do equations with numbers from [0, i] */

for(z = 0; z < i; z++)
{
product *= numbers[z];
sum += numbers[z];
}

/* etc */```
of course that's purely a demonstration.

3. here is untested code
now you figure it out what should be the variable types
Code:
```   /*everytime you get a new number do this*/
sum = sum + num;
product = product*num;
inverse_sum = inverse_sum + 1/((float)num);
num_count = num_count + 1;
/*num_count is the total number of numbers entered*/

/* calculate the mean  as follows */
printf("Average: &#37;f\n",sum/((float)num_count));
printf("H mean : %f\n",num_count/inverse_sum);
printf("G mean: %f\n", pow(product,1/(float)num_count);```
include math.h in your code & your compile command should have -lm..
if your file is named as mean.c then the command will be like
Code:
`\$cc -o mean -lm mean.c`
THIS CODE HAS LIMITATIONS. I THINK YOU MIGHT BE KNOWING THEM. IF NOT KNOW THEM FIRST

4. Don't give away the solution, especially if this is homework - it only encourages people to beg for work to be done for them.

> printf("G mean: &#37;f\n", powf((float)product,1/(float)num_count);
Missing something here...? Also why would product need to be a double if you're going to cast it back to float anyway?

5. oops!!.. sorry..
I changed it now..

about the float: I didnt look into it much.. useless typechecking

6. Thanks for the help. I have a better idea of what I need to do now.