The instruction is
指令是
Implement the method
public static double avg(int[][] a)
, which returns the average of the values stored ina
.实现方法public static double avg(int [] [] a),它返回存储在a中的值的平均值。
So, I guess I have to write
所以,我想我必须写
public static double avg(int[][] a) { // two dimensional arrays?
// some codes
return a;
}
I don’t understand the sentence “which returns the average of the values stored in a”. When I don’t even know the values stored in a
, how can I get the average?
我不明白句子“返回存储在a中的值的平均值”。当我甚至不知道存储在a中的值时,我怎样才能获得平均值?
3 个解决方案
#1
4
The function takes a 2d array as an argument. I believe you are supposed to do 2 for loops over a and sum each item and then divide by number of total items in the end (average). Then return your double (average) variable, not return a as your code shows.
该函数采用2d数组作为参数。我相信你应该在a上做2个循环并对每个项目求和,然后除以最终的总项目数(平均值)。然后返回你的双(平均)变量,而不是像代码所示那样返回a。
#2
2
yes I agree with David, you will need 2 nested for loops to add all the values and return it.
是的我同意David的意见,你需要2个嵌套for循环来添加所有值并返回它。
something like...
就像是...
public static double avg(int [][] a)
{
double sum = 0;
int total = 0;
for(int i = 0; i < a.length; i++){
total += a[i].length;
for (int j = 0; j < a[i].length; j++){
sum+=a[i][j];
}
}
return sum/total;
}
and then divide the sum by number of elements.
然后将总和除以元素数。
#3
1
With a double array you have to walkthrough all the items. The program will count the amount of values in the array and sum all values. To get the average you have to devide the total by the amount of values.
使用双数组,您必须遍历所有项目。程序将计算数组中的值的数量并将所有值相加。要获得平均值,您必须将总数除以值。
public static double avg(int[][] a)
{
int amountOfValues = 0;
int total = 0;
for(int y = 0; y < a.length; y++)
{
for(int x = 0; x < a[y].length; x++)
{
total += a[y][x];
amountOfValues++;
}
}
return (double)(total)/(double)(amountOfValues);
}
#1
4
The function takes a 2d array as an argument. I believe you are supposed to do 2 for loops over a and sum each item and then divide by number of total items in the end (average). Then return your double (average) variable, not return a as your code shows.
该函数采用2d数组作为参数。我相信你应该在a上做2个循环并对每个项目求和,然后除以最终的总项目数(平均值)。然后返回你的双(平均)变量,而不是像代码所示那样返回a。
#2
2
yes I agree with David, you will need 2 nested for loops to add all the values and return it.
是的我同意David的意见,你需要2个嵌套for循环来添加所有值并返回它。
something like...
就像是...
public static double avg(int [][] a)
{
double sum = 0;
int total = 0;
for(int i = 0; i < a.length; i++){
total += a[i].length;
for (int j = 0; j < a[i].length; j++){
sum+=a[i][j];
}
}
return sum/total;
}
and then divide the sum by number of elements.
然后将总和除以元素数。
#3
1
With a double array you have to walkthrough all the items. The program will count the amount of values in the array and sum all values. To get the average you have to devide the total by the amount of values.
使用双数组,您必须遍历所有项目。程序将计算数组中的值的数量并将所有值相加。要获得平均值,您必须将总数除以值。
public static double avg(int[][] a)
{
int amountOfValues = 0;
int total = 0;
for(int y = 0; y < a.length; y++)
{
for(int x = 0; x < a[y].length; x++)
{
total += a[y][x];
amountOfValues++;
}
}
return (double)(total)/(double)(amountOfValues);
}