Tomahok
Tomahok

Reputation: 9

What is the bug in my "find median" implementation?

I found someone who had a similar problem (How to calculate the median of an array?), but I couldn't figure out how to incororate it in to my own code since I am rather new to java. Right now, my findmedian method is returning 0 instead of the actual median and I can't seem to figure it out. Thanks!

import java.util.Scanner;
import java.util.Arrays;

public class Original
{
    public static void main(String[] args)
    {
        Scanner inputNumber = new Scanner(System.in);
        Scanner dataItem = new Scanner(System.in);
        Scanner input = new Scanner(System.in);

        System.out.print("This stores a list of contirbutions to a charity drive.\n ");
        System.out.print("How many contributors will be entered? ");

        double contributors = inputNumber.nextDouble();
        double contributions[ ] = new double[50];  
        double contributions_check[] = findData (contributors, contributions);

        System.out.print("See if the contributions are correct. ");

        // Displays the contributions, loop allows numbers to be displayed correctly
        for (int count = 0; count < contributors; count++) {
            System.out.print(contributions_check[count] + "    ");
        }

        double median = findmedian(contributors,contributions_check);
        System.out.print("\n The median contribution is: " + median);
    }

    public static double[] findData(double n, double[] contributions2)
    {
        Scanner dataItem = new Scanner(System.in);

        // x must be 0 and x must be < than n
        for (int x = 0; x < n; x++) {
            System.out.print("Please enter the next contribution: ");
            contributions2[x] = dataItem.nextDouble();
        }

        return contributions2;
    }

    public static double findmedian(double n, double data[])
    {
        Arrays.sort(data);
        double median;

        if (data.length % 2 == 0) {
            median = ((double) data[data.length / 2] +
                      (double) data[data.length / 2 - 1]) / 2;
        } else {
            median = (double) data[data.length/2];
        }

        return median;
    }
}

Upvotes: 0

Views: 1589

Answers (2)

Ace McCloud
Ace McCloud

Reputation: 900

Use the number of contributors n to know the valid contributors in your array.

  public static double findmedian(double n, double data[])
        {
            Arrays.sort(data);
            double median;

            if (data.length % 2 == 0) {
                median = ((double) data[n / 2] +
                          (double) data[n / 2 - 1]) / 2;
            } else {
                median = (double) data[n/2];
            }

            return median;
        }

Upvotes: 0

AShelly
AShelly

Reputation: 35540

I think the issue is you are using data.length in findmedian, where you should be using n. data.length is always going to be 50, even if you only entered 5 items....

Upvotes: 2

Related Questions