0 Replies - 1804 Views - Last Post: 08 April 2012 - 06:08 AM

#1 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Calculate the Harmonic Mean

Posted 08 April 2012 - 06:08 AM

Description: See Example Usage in snippetThe harmonic mean (sometimes called the subcontrary mean) is one of several kinds of average. Typically, it is appropriate for situations when the average of rates is desired.
public class Means
{
    // Returns the harmonic mean of a data set
    public static double harmonicMean(double[] data)
    {
	double sum = 0.0;

	for (int i = 0; i < data.length; i++)
	{
	    sum += 1.0 / data[i];
	}

	return data.length / sum;
    }

    public static void main(String[] args)
    {
	double[] x = { 2, 3, 4, 5, 6, 8, 10, 11.53542 };

	System.out.println("Harmonic Mean = " + Means.harmonicMean(x));
    }
}

// OUTPUT

// Harmonic Mean = 4.541095305474111


Is This A Good Question/Topic? 0
  • +

Page 1 of 1