A
A
Amigo20192019-02-17 17:49:49
Java
Amigo2019, 2019-02-17 17:49:49

How to pass the rendered array?

Good afternoon!
I need help, I can’t either drive the output array into the method, or correctly call the method with the correct parameters.

import java.util.Scanner;
import static java.lang.Math.*;
public class OneDimensionalArray {
          //создание и заполнение массива
    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);
            }
        }
        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();
        }
        return 0;
        }
    //задание найти среднее арифметическое - сумма всех чисел деленная на их количество
     static double mean(double[][] array) {
        double total=0;
        int totallength = 0;
        for(int i=0;i<array.length;i++) {
            for(int j=0;j<array[i].length;j++) {
                total += array[i][j];
                totallength++;
            }
        }
        return total/(totallength);
    }
                //майн
    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("Матрица :  " );
        createRandomArray(size);
        System.out.println("Среднее арифметическое :  " );
        
    }


}

5c697489d4561888923490.png
The array is displayed, but there is a problem with the average over the array, the method seems to be correct, but there is a problem with its use.
Wait, in the code, the method call itself was removed at the very end. If anyone can help, thank you..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVGENY T., 2019-02-18
@Amigo2019

Your double[][] da = new double[size][size] is not visible anywhere else except in the createRandomArray method.
The method must return an array in return or in parameters.
Or make da a global variable and use it in all methods, but that would be bad manners.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question