waltermilner

Untitled

Apr 14th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. double arithMean(double values[], int length)
  6. {
  7.     double sum=0;
  8.     for (int i = 0; i<length; i++)
  9.         sum+=values[i];
  10.  
  11.     return sum/length;
  12. }
  13.  
  14. double geomMean(double values[], int length)
  15. {
  16.     double product=1;;
  17.     for (int i = 0; i<length; i++)
  18.         product*=values[i];
  19.  
  20.     product = pow(product, 1.0/length);
  21.     return product;
  22. }
  23.  
  24. double harmMean(double values[], int length)
  25. {
  26.     double sum=0;
  27.     for (int i = 0; i<length; i++)
  28.         sum+=1.0/values[i];
  29.  
  30.     return length/sum;
  31. }
  32.  
  33. int main()
  34. {
  35.     double data[]= {23,37,14,8,64,52,32,18};
  36.     int size=8;
  37.     printf("Arithmetic mean = %f\n", arithMean(data, size));
  38.     printf("Geometric mean = %f\n", geomMean(data, size));
  39.     printf("Harmonic mean = %f\n", harmMean(data, size));
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment