[英]how do you find the max row in a 2d array
Create 2-D array say m : 创建二维数组说m:
Pass the above array m and call the following two methods 传递上面的数组m并调用以下两个方法
findMaxRow(double[][]array)
, find and print the largest sum of columns in the 2D array findMaxRow(double[][]array)
,找到并打印2D数组中最大的列总和 returnAvg(m)
, print out the avg of array m
returnAvg(m)
,打印出数组m
的平均值 Comment: 评论:
So, I made the the code to find the max Colm but I can't figure out how to find the max Row. 因此,我编写了代码以找到最大Colm,但是我不知道如何找到最大Row。 I need to be able to find the Max Row but I am figuring out how to since my code finds the cllm and not the max row.
我需要能够找到“最大行”,但是由于我的代码找到了cllm而不是最大行,因此我正在寻找方法。
Here is my code:import java.util.Scanner; 这是我的代码:import java.util.Scanner;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
System.out.print("Number of Rows? ");
int rows = input.nextInt();
System.out.print("Number of Columns? ");
int columns = input.nextInt();
while (columns > 7) { // check for column > n+5 where n = 2 in my case
System.out.print("The column amount is too high. Try another number: ");
columns = input.nextInt();
}
double[][] m = new double[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
m[i][j] = rand.nextDouble()*7+4;
}
}
findMaxCol(m);
System.out.println("The average value of this array is "+returnAvg(m));
}
public static void findMaxCol(double[][] a) {
double[] maxCol = new double[a[0].length];
double max = 0;
int maxColNum=0;
for (int i = 0; i < a[0].length; i++) { // Sum of Columns
for (int j = 0; j < a.length; j++) {
maxCol[i]+=a[j][i];
}
}
//System.out.println(Arrays.toString(maxCol));
for (int i = 0; i < maxCol.length; i++) {
if (max < maxCol[i]) {
max = maxCol[i];
maxColNum = i; // column number will associate with its array column starting with 0
}
}
System.out.println("The highest column sum is Column #"+maxColNum+" with a sum of "+max);
}
public static double returnAvg(double[][] a) {
double sum = 0; // initialization
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
sum+=a[i][j];
}
}
// System.out.println(sum); Test line
return (sum/(a.length*a[0].length)); // avg
}
} }
Not sure I understand your desired result, but this should give you the row with the greatest sum, as well as what that sum was. 不确定我是否了解您想要的结果,但是这应该为您提供最大金额的行以及该金额是多少。 Note this doesn't take into account multiple rows with equal sums.
请注意,这并未考虑总和相等的多行。 It will report the first row in the array with the greatest sum.
它将报告总和最大的数组中的第一行。
public static void findMaxRow(double[][] a){
double maxSum = 0;
int maxRow = 0;
for (int row = 0; row < a.length; row++){
double rowSum = 0;
for (int column = 0; column < a[row].length; column++){
rowSum += a[row][column];
}
if (rowSum > maxSum){
maxSum = rowSum;
maxRow = row;
}
}
// maxSum should be the greatest sum of any row
// maxRow is the row that contained the greatest sum
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.