A
A
Amigo20192019-02-18 22:16:18
Java
Amigo2019, 2019-02-18 22:16:18

Why doesn't it show the averages of the rows and columns?

Good evening!
The print method does not display the averages of columns and columns. It seems that something is wrong in the print or mean method, something is missing..

import java.util.Scanner;

    public class TwoDimensionalArray {
        //создание и заполнение массива
        static double[][] createRandomArray(int size) {
            double[][] da = new double[size][size];
            for(int i=0; i<da.length; i++) {
                for(int j=0; j<da[i].length; j++) {
                    da[i][j] = (Math.random()*100);
                }
            }
            return da;
        }
        //задание найти среднее арифметическое - сумма всех чисел деленная на их количество
        static double mean(double[] onearray) {
            double total=0;
            int totallength = 0;
            for(int i=0;i<onearray.length;i++) {
                 {
                    total += onearray[i];
                    totallength++;
                }
            }
            return total/(totallength);
        }
        //print
        static double[][] print(double[][] da){
            for(int i=0; i<da.length; i++) {
                for(int j=0; j<da[i].length; j++) {
                    System.out.print(da[i][j] + "\t");
                    System.out.println("Среднее значение "+"["+i+"]"+" строки: "+"["+mean(double[] (da)[i]+"]");
                    System.out.println("Среднее значение "+"["+j+"]"+" cтолбца "+"["+mean(double[] (da)[j]+"]");
                }
                System.out.println();
            }
            return da;
        }
        //майн
        public static void main(String args[]) {
            Scanner num = new Scanner(System.in);
            System.out.println ("Введите размер матрицы ");
            int size= num.nextInt();//считывает число и присваивает значение в size
            System.out.println("Вы ввели  размер "    +size);
            System.out.println("Матрица :  " );
            double[][] array=new double[size][size];
            array = createRandomArray(size);
            print(array);
        }
    }

I will be glad for any help! Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Ksenia, 2019-02-19
@Amigo2019

This is my vision of the situation. C# language, if that.

// MeanLineValue вернет массив, состоящий из чисел, каждое из которых – среднее значение по строке
    public float[] MeanLineValue(float[,] array)
    {
        // Создаем массив нужной длины (т.е. такой длины, сколько строк нам нужно обсчитывать)
        float[] values = new float[array.GetLength(0)];
        // Складываем значения
        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                values[i] += array[i, j];
            }
            // Ищем среднее для каждой строки
            values[i] /= array.GetLength(1);
        }
        // Возвращаем массив со средними значениями по строкам
        return values;
    }

// Создаем массив
int size = 3;
float[,] array = new float[size, size * 2];
// Заполняем рандомными значениями
for (int i = 0; i < array.GetLength(0); i++)
    {
        for (int j = 0; j < array.GetLength(1); j++)
        {
            array[i, j] = Random.value * 10f;
         }
    }
// Выводим средние по строкам
for (int i = 0; i < size; i++)
{
    Debug.Log(MeanLineValue(array)[i]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question