Create a 4x4 arrangement in java

1

I need to create an arrangement with the following numbers and calculate:

By line: The average and the standard deviation

The average and standard deviation of the entire arrangement

  

{3,8,7,6} {7,5,7,4} {3,2,1,0} {9,6,4,8}

This is what I'm carrying but it does not work on the deviation part and apparently it just copes with the whole arrangement

public class arreglo4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int data[][] = { { 3, 8, 7, 6 }, { 7, 5, 7, 4 }, { 3, 2, 1, 0 }, { 9, 6, 4, 8 } };

        // acumulador
        double sum = 0;
        for (int i = 0; i < data.length; i++) {
            sum = sum + data.length;
        }

        // desplegar
        for (int ren = 0; ren < data.length; ren++) {
            for (int col = 0; col < data[ren].length; col++) {
                System.out.println(data[col][ren] + "\t");
            }
            System.out.println();
        }
        // promedio

        double mean = sum / data.length;
        System.out.println("promedio " + mean);

        // Desviacion

        double sum1 = 0;
        for (int i = 0; i < data.length; i++) {
            sum1 = sum1 + ((data[i]-mean) * (data[i]-mean));
        }
        double s=Math.sqrt(sum1/(data.length-1));
        System.out.println("La desviacion es " +s);
    }

}
    
asked by TGAB99 17.04.2018 в 05:01
source

3 answers

0

This is how a multidimensional array is declared:

int matriz[][] = new int[4][4];

You can declare and initialize it at the same time as follows:

int matriz[][] = {{3,8,7,6}, {7,5,7,4}, {3,2,1,0}, {9,6,4,8}}

You must post how you calculate what you need and the code of how you tried to do it.

    
answered by 17.04.2018 в 05:57
0

I understand that you want to get the average of each of the sub-arrays and then make the average of all of these. To create multidimensional arrays (arrays), you must write:

int matriz[][] = new int [4][4]
//tambien crearemos unas variables para guardar las medias
int mediaTotal = 0;
int subMedia = 0;

To be able to move through them you must create a double for (nested for):

for(i=0;i<matriz.lenght;i++){
   for(j=0;j<matriz.lenght;i++){
//aqui harias la media de cada sub-array
submedia += matriz[i][j];
}
System.out.println("media de sub-array "+i+": "+subMedia)
mediaTotal += submedia/2;
} 
System.out.println("media total de todos los arrays: "+mediaTotal);

As for the deviations ... I do not know what you mean, I hope I helped you. An upvote is appreciated: P

    
answered by 17.04.2018 в 06:38
0

To solve this problem you can use the streams of java 8 and a collector to calculate the standard deviation

 int data[][] = { { 3, 8, 7, 6 }, { 7, 5, 7, 4 }, { 3, 2, 1, 0 }, { 9, 6, 4, 8 } };
    Arrays.stream(data).mapToDouble(x ->  Arrays.stream(x).
            average().
            orElse(0) ).
            forEach(s -> System.out.println("promedio "+s));

    double average = Arrays.stream(data).
            flatMapToInt(Arrays::stream).
            average().orElse(0);
    System.out.println("promedio total "+average);

    Arrays.stream(data).mapToDouble(x ->  Arrays.stream(x).
            mapToDouble((int s) -> 1.0 * s).
            boxed().
            collect(DoubleStatistics.collector())).
            forEach(s -> System.out.println("desviacion estandar "+s));

    double r = Arrays.stream(data).
                    flatMapToInt(Arrays::stream).
                    mapToDouble((int s) -> 1.0 * s).
                    boxed().
                    collect(DoubleStatistics.collector());

    System.out.println("desviacion estandar total "+r);

and the collector code

public class DoubleStatistics extends DoubleSummaryStatistics {

    private double sumOfSquare = 0.0d;
    private double sumOfSquareCompensation; // Low order bits of sum
    private double simpleSumOfSquare; // Used to compute right sum for
    // non-finite inputs

    @Override
    public void accept(double value) {
        super.accept(value);
        double squareValue = value * value;
        simpleSumOfSquare += squareValue;
        sumOfSquareWithCompensation(squareValue);
    }

    public DoubleStatistics combine(DoubleStatistics other) {
        super.combine(other);
        simpleSumOfSquare += other.simpleSumOfSquare;
        sumOfSquareWithCompensation(other.sumOfSquare);
        sumOfSquareWithCompensation(other.sumOfSquareCompensation);
        return this;
    }

    private void sumOfSquareWithCompensation(double value) {
        double tmp = value - sumOfSquareCompensation;
        double velvel = sumOfSquare + tmp; // Little wolf of rounding error
        sumOfSquareCompensation = (velvel - sumOfSquare) - tmp;
        sumOfSquare = velvel;
    }

    public double getSumOfSquare() {
        double tmp = sumOfSquare + sumOfSquareCompensation;
        if (Double.isNaN(tmp) && Double.isInfinite(simpleSumOfSquare)) {
            return simpleSumOfSquare;
        }
        return tmp;
    }

    public final double getStandardDeviation() {
        long count = getCount();
        double sumOfSquare = getSumOfSquare();
        double average = getAverage();
        return count > 0 ? Math.sqrt((sumOfSquare - count * Math.pow(average, 2)) / (count - 1)) : 0.0d;
    }

    public static Collector<Double, DoubleStatistics, Double> collector() {
    return Collector.of(DoubleStatistics::new,
            DoubleStatistics::accept, DoubleStatistics::combine, DoubleStatistics::getStandardDeviation);
}

}
    
answered by 18.04.2018 в 00:32