Sunday, November 20, 2011

The Arithmetic, Geometric and Harmonic Mean of an Array in ANSI C

Before defining the functions which shall compute the means, we shall create 2 functions which will help us compute the elemental sum and the elemental product of the array. These functions will be used by the mean functions.
/**
 * Description:
 *  Computes the sum of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The sum of all elements from the array.
 */
double ArraySum(double* array, int size)
{
   double sum = 0.0;
   int i;
   for(i=0; i<size; i++)
   {
      sum+=array[i];
   }
   return sum;
}
/**
 * Description:
 *  Computes the product of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The product of all elements from the array.
 */
double ArrayProduct(double *array, int size)
{
   double product = 1.0;
   int i;
   for(i=0; i<size; i++)
   {
      product*=array[i];
   }
   return product;
}
1.The Arithmetic Mean

The arithmetic mean is computed using the following formula:
where n represents the size of the array and xi represents the element at the i index. The implementation for the function is:
/**
 * Description:
 *  Computes the arithmetic mean of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The arithmetic mean of all elements from the array.
 */
double ArithmeticMean(double* array, int size)
{
   double result = ArraySum(array,size)/size;
   return result;
}
2.The Geometric Mean

The geometric mean is computed by using the following formula:
where n represents the size of the array and xi represents the element at the i index. The implementation for the function is:
/**
 * Description:
 *  Computes the geometric mean of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The harmonic mean of all elements from the array.
 */
double GeometricMean(double* array, int size)
{
   double result = pow(ArrayProduct(array,size),
                   (1.0/(double)(size)));
   return result;
}
3.The Harmonic Mean
The harmonic mean is computed by using the following formula:
where n represents the size of the array and xi represents the element at the i index. The implementation for the harmonic mean function would be:
/**
 * Description:
 *  Computes the harmonic mean of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The geometric mean of all elements from the array.
 */
double HarmonicMean(double* array, int size)
{
   double result = 0;
   int i;
   for(i=0; i<size; i++)
   {
      result+=(1.0)/array[i];
   }
   result = (double)(size)*pow(result,-1.0);
   return result;
}
4. Example
#include<stdio.h>
#include<math.h>

double ArraySum(double* array, int size);
double ArrayProduct(double *array, int size);
double ArithmeticMean(double* array, int size);
double GeometricMean(double* array, int size);
double HarmonicMean(double* array, int size);

int main(void)
{
   /*Declaring an array which will hold 9 elements*/
   double array[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0,7.0, 8.0, 9.0};
   /*Printing the results of the functions*/
   printf("Arithmetic mean = %lf\n"
          "Harmonic mean   = %lf\n"
          "Geometric mean  = %lf\n",
          ArithmeticMean(array,9),
          HarmonicMean(array,9),
          GeometricMean(array,9));
   return 0;
}
/*Output
Arithmetic mean = 5.000000
Harmonic mean   = 3.181372
Geometric mean  = 4.147166
 */

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger