Geometric Mean

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Geometric Mean

  1. #1
    Join Date
    Oct 2004

    Exclamation Geometric Mean

    Hi folks, can anyone help me with a problem?

    I have a class below to work out the average of a series of numbers. Can anyone give me a clue on how to extend this example so that it also prints the geometric mean of the numbers. The geometric mean of n numbers is the n-th root of their product. Using
    the Math.pow function to calculate the n-th root.

    All hints, solutions appreciated. code below:

    // Average
    // Accept a series of numbers and display average (0 ends input and does not count)

    import javax.swing.JOptionPane; // Required for Windows boxes

    public class Average
    public static void main(String args[])
    // Declare variables
    int[] digit = new int[100];
    int[] nums;
    String input = "";
    String output = "";
    int count = 0;
    int index = 9;
    int amount = 0;
    int sum = 0;
    int avg = 0;

    // Request input
    while(index != 0)
    input = JOptionPane.showInputDialog(null, "Enter an Integer", "Average Calculator", JOptionPane.QUESTION_MESSAGE);
    index = Integer.parseInt(input);
    digit[count] = index;


    // Calculate average

    for (count = 0; count < digit.length; count++) // for every element in digit[]
    if (digit[count] > 0) // only if not equal to 0
    amount++; // running total of live numbers


    nums = new int[amount]; // assign new length to hold stored numbers
    for (int i = 0; i < nums.length; i++)
    nums[i] = digit[i]; // transfer only live values
    sum += nums[i]; // get total of live values
    if(amount > 0) // don't divide by zero pc wont like it!
    avg = (sum / amount); // Finally get the average integer
    JOptionPane.showMessageDialog(null, "You Just Entered 0", "Nothing to calculate", JOptionPane.ERROR_MESSAGE);
    System.exit(0); // Break out of program

    // Create output
    output = "The sum of the integers you entered is: " + sum + "\nThe average integer you entered is: " + avg;

    // Output result
    JOptionPane.showMessageDialog(null, output, "Average Calculator", JOptionPane.INFORMATION_MESSAGE);

    // End program

  2. #2
    Join Date
    Sep 2004
    well if i understand the geometric mean correctly, then something like this should be ok

    noOfNumbers = //get the number of numbers entered
    double multiplyResult = 1.0;
    double geoMean;
    //this will multiply all numbers entered in by user
    for(int i=0;i<digit.length;i++){
      multiplyResult = multiplyResult*digit[i];
    geoMean = Math.pow(multiplyResult, (1/noOfNumbers));
    this is NOT tested, just an idea about how to go about it, you could change the code all you want, but it should be on the right track
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Oct 2004

    Kram Kram your the man if you cant do it no one can

    Cheers pal.

    haven't had time to test it yet but looks logical.

    -- Life is so much easier when someone else has the answer --

  4. #4
    Join Date
    Sep 2004
    no problem matey
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center