menu

arrow_back How to pass the array?

by
2 votes
Good day!
Need help does not work, whether to drive the output array to the method,whether to pravilnosti 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

Массив выводится,а вот со средним по массиву проблема,метод вроде верен,но вот с его использованием проблема.

Счас в коде сам вызов метода в самом конце убрал . Если кто может помочь,то спасибо..

9 Comments

Xenia ,
Thanks,took your advice
But I think again something wrong.The arithmetic mean my Nan comes out

import java.util.Scanner;

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 da;
}
//задание найти среднее арифметическое - сумма всех чисел деленная на их количество
static double mean(double[][] array) {
double total=0;
int totallength = 0;
double sred=total/(totallength);
for(int i=0;i<array.length;i++) {
for(int j=0;j<array[i].length;j++) {
total += array[i][j];
totallength++;
}
}
return sred;
}
//майн
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);
System.out.println("Среднее арифметическое : " +mean (array));


}


}

5c699dbbe3255721830821.png
Tolley whether
Xenia ,
Everything turned out thank you very much!!!!!!!!!!!
Virtual you a cake for tea :-)
5c69a2b5c7369604747337.jpeg
Amigo2019 ,
Return needed because your method:
double createRandomArray(int size);
double - in this case the type of the value that method returns. You should not double and a two-dimensional array of double. You want to get the result as an array? Yes. Then return the array.

Instead of:
return 0;
Need return da;
(return array)

Then in main create an array and then:
array = createRandomArray(size);
createRandomArray by your definition returns a two-dimensional array.
Xenia most likely you are right in principle, right..
The idea began to demand that the right of return so I put a withdrawal has occurred and it seemed that everything is in order..
I do not understand it,or rather not vychislenii and send the results..
several options
mean();using print,mean(double[][] da);
why in the method createRandomArray() you return 0?
You have some confusion in your code.

To begin, decide what you are doing.

1. I want to create a random array of a given size. So "size" is the input "array" is output.
2. Want to find the arithmetic mean of the array. So "array" is input, "average" output.
3. All Sorts Of System.out.do a println in main then it is. Move them out createRandomArray, so as not to clutter up the code.
the method seems correct,but with its use the problem.

The method is correct. With its use the problem of school createRandomArray and the lack of understanding of how to organize the calculation chain.
In Java is not required to explicitly convert the type? Type return total/ double(totallength); or how it is done.
but with its use the problem.
What is it? Show how you are trying to use your method mean.

1 Answer

by
 
Best answer
0 votes
You have a double[][] da = new double[size][size] is not visible anywhere else except for the method of createRandomArray.

The method should return an array in the return or in the parameters.

Or make da global variable and rotate it in all methods, but it will be bad.