Program in java to calculate fashions of a sample

0

I have the code to calculate the fashion of a population sample but I do not know what to do if it is a sample of two or more fashions.

public static void getModa(double muestra[]) {

    int maximoNumRepeticiones= 0;
    double moda= 0;

    for(int i=0; i<muestra.length; i++)
    {
        int numRepeticiones= 0;
        for(int j=0; j<muestra.length; j++)
        {
            if(muestra[i]==muestra[j])
            {
                numRepeticiones++;
            }   //fin if
            if(numRepeticiones>maximoNumRepeticiones)
            {
                moda= muestra[i];
                maximoNumRepeticiones= numRepeticiones;
            }   //fin if
        }
    }   //fin for
    System.out.print(moda);
}   //fin getModa

This is what I hope you do: Entry: double muestra= new double {3, 4, 3, 3, 5, 4, 4} Output: moda= 3 4

This is what it does: Entry: double muestra= new double {3, 4, 3, 3, 5, 4, 4} Output: moda= 3

I'm supposed to have two fashions: 3 and 4. I could even have more but only show me the first one.

    
asked by DiegoR2 25.08.2017 в 19:02
source

1 answer

-1

You can use an ArrayList to add samples, and with the following method you can use the one you already have defined but so that now you calculate the fashion of a list of samples. Below I add another method that is a test of this scheme that I propose, and a link to the API of ArrayList.

public static void getModas(java.util.ArrayList<double[]> listaDeMuestras) {

    if (listaDeMuestras != null) {
        for (int i = 0; i<listaDeMuestras.size(); i++) {
            System.out.println("procesando una muestra");
            getModa(listaDeMuestras.get(i));
        }
    }
}

public static void prueba3Muestras() {

    double[] muestra1 = new double[] {3, 41, 3, 3, 5, 4, 4};
    double[] muestra2 = new double[] {23, 4, 83, 93, 5, 4, 4};
    double[] muestra3 = new double[] {93, 4, 3, 3, 55, 64, 4};

    java.util.ArrayList<double[]> lista = new java.util.ArrayList();

    lista.add(muestra1);
    lista.add(muestra2);
    lista.add(muestra3);

    getModas(lista);

}

link

    
answered by 21.11.2017 в 20:59