I thought I might post this site to let those who care know my "next
project". I will be looking at harmonic and geometric means and fincial
analysis and economics. I am going to try to work out geometric and harmonic
means myself in C. If I have a problem will ask for help.
Perhaps this can give you a leg up:
/* Steve Summit's C programming
*
* Section 3 :: exercise 2
*
* STATEMENT: Write a program to compute the average of the ten
numbers 1, 4,
* 9, ..., 81, 100, that is, the average of the squares of the
numbers from
* 1 to 10. (This will be a simple modification of Exercise 3 from
last
* week: instead of printing each square as it is computed, add it in
to a
* variable sum which keeps track of the sum of all the squares, and
then at
* the end, divide the sum variable by the number of numbers summed.)
*
*/
#include <stdio.h>
#include <math.h>
#include <assert.h>
double geometricMean(size_t count, double vector[])
{
double sum = vector[0];
size_t index;
if (count == 0)
return 0;
for (index = 1; index < count; index++)
sum *= vector[index];
return pow(sum, 1.0 / count);
}
/* Ref:
http://mathworld.wolfram.com/PowerMean.html */
/* CANNOT be used on negative quantities! */
double generalizedMean(double power, size_t count, double
vector[])
{
size_t index;
double sum = 0;
if (count == 0)
return 0;
if (power == 0)
sum = geometricMean(count, vector);
else {
for (index = 0; index < count; index++) {
assert(vector[index] > 0);
sum += pow(vector[index], power);
}
sum /= count;
sum = pow(sum, 1.0 / power);
}
return sum;
}
double arithmeticMean(size_t count, double vector[])
{
double sum = 0;
size_t index;
if (count == 0)
return 0;
for (index = 0; index < count; index++)
sum += vector[index];
return sum / count;
}
double harmonicMean(size_t count, double vector[])
{
double sum = 0;
size_t index;
if (count == 0)
return 0;
for (index = 0; index < count; index++)
sum += 1.0 / vector[index];
sum /= count;
return 1.0 / sum;
}
double rmsMean(size_t count, double vector[])
{
double sum = 0;
size_t index;
if (count == 0)
return 0;
for (index = 0; index < count; index++)
sum += vector[index] * vector[index];
sum /= count;
return sqrt(sum);
}
int main()
{
int i;
double vector[10];
for (i = 1; i <= 10; ++i) {
vector[i - 1] = i * i;
}
printf("The Arithmetical average is: %.1f\n", generalizedMean
(1.0, 10, vector));
printf("Checking.....................: %.1f\n\n", arithmeticMean
(10, vector));
printf("The Harmonic average is: %.1f\n", generalizedMean
(-1.0, 10, vector));
printf("Checking.....................: %.1f\n\n", harmonicMean(10,
vector));
printf("The Geometric average is: %.1f\n", generalizedMean
(0.0, 10, vector));
printf("Checking.....................: %.1f\n\n", geometricMean
(10, vector));
printf("The RootMeanSquare average is: %.1f\n", generalizedMean
(2.0, 10, vector));
printf("Checking.....................: %.1f\n\n", rmsMean(10,
vector));
return 0;
}