# Thread: Average, Geo and Harmonic mean calcl

1. ## Average, Geo and Harmonic mean calcl

Hello,
I am writing a program to read a series of numbers then calculate average, geometric mean and harmonic mean. I'm not sure if my formulas are entered correctly. Any thoughts, (not good at math)
Code:
```/*this program will calculate the average, geometric mean and harmonic mean of a series of 5 pos numbers.
written by: Patricia Stephens
Date: June 2009
*/

#include<stdio.h>
#include "stdafx.h"
#include "math.h"

#define M 5
int main(void)
{
//local declarations
int i;
int sum;
int avg;
int n;
sum=avg=0;

//Statements

printf("please enter 5 whole numbers then key <EOF> to stop(CTRL+D OR CTRL + Z\n");
for (i=0; i<=M; i++)
{
scanf_s("%2d",&n);
sum=sum+n;

}
printf("the sum is %2d\n", ((sum)-n));
printf("the average is %2d\n", (int(sum)/n));
printf("the harmonic mean is %f\n", (n/float(sum)/1));
printf("the geometric mean is %f\n", pow(sum,1.0/n));

return 0;

}//main```

2. The harmonic mean is the reciprocal of the mean of the reciprocals. You're going to have to do each bit separately -- you can't use the sum of the numbers to get the sum of the reciprocals. (That is, 1+2+3+4+5 = 15, and 1/1+1/2+1/3+1/4+1/5 = 137/60, which you can't really get to from 15.)

Similarly the geometric mean requires the product of the numbers, not their sum.

3. All of the means are of the same form:

Mean[F]( x[0..N] ) = F^-1( Sum( F( x[n] ), n=0..N ) / N )

They differ only in the definition of F.

For average, F(x)=x
For geometric, F(x)=log(x)
For harmonic, F(x)=1/x

4. Oh and also if you didn't run through your loop six times you wouldn't have to get rid of the excess number at the end.

Originally Posted by brewbuck
All of the means are of the same form:

Mean[F]( x[0..N] ) = F^-1( Sum( F( x[n] ), n=0..N ) / N )

They differ only in the definition of F.

For average, F(x)=x
For geometric, F(x)=log(x)
For harmonic, F(x)=1/x
Don't give me homework assignment ideas like that.

5. Sorry about the homework assignment idea Just need some help(alot)
I noticed the excess number but wasn't sure what to do about it. Just learning and not real good with all of it just yet.
I see what you mean(no pun) about the sum of the product(geometric mean) just not sure how to code it.
Never had algebra, probably should've before taking this.