I am trying to print the maximum sum of a row from a 2d-array but it is looping the printing info I am not sure why since my minimum sum method is the same and that one is working fine as in not looping the print line. Also, the reason why I am not just printing it out in the main is that I have to print the row it is in.
UPDATE: SOLVED
The output that's happening with the maximum sum:
row 1 has the highest sum: 200.0
row 2 has the highest sum: 1990.0
row 4 has the highest sum: 2200.0
row 5 has the highest sum: 2250.0
the output I want:
row 5 has the highest sum: 2250.0
public static double findMaximum(double [][] arr) {
double maxSum = Double.MIN_VALUE;
for (int i= 0; i < arr.length; i ) {
double sum = 0.0;
for (int j = 0; j < arr[i].length; i ) {
sum = arr[i][j];
}
if (maxSum < sum) {
maxSum = sum;
System.out.println("row " (i 1) "has the highest sum is " maxSum);
}
}
return maxSum;
public static double findMin(double [][] arr) {
double minSum = Double.MAX_VALUE;
for (int i = 0; i < arr.length; i ) {
double sum = 0.0;
for (int j = 0; j < arr[i].length; j ) {
sum = arr[i][j];
}
if (minSum > sum) {
minSum = sum;
System.out.println("row " (i 1) " has the lowest sum : " minSum);
}
}
return minSum;
}
CodePudding user response:
All you need is just retrieve a row index along with sum of the row.
public static void main(String[] args) {
double[][] arr = {
{ 9, 10, 11, 12 },
{ 5, 6, 7, 8 },
{ 1, 2, 3, 4 } };
Pair maxSum = findSum(arr, Pair.SORT_BY_SUM_ASC.reversed());
Pair minSum = findSum(arr, Pair.SORT_BY_SUM_ASC);
System.out.format(Locale.ENGLISH, "row %d has the highest sum is %.2f\n", maxSum.row 1, maxSum.sum);
System.out.format(Locale.ENGLISH, "row %d has the lowest sum is %.2f\n", minSum.row 1, minSum.sum);
}
private static final class Pair {
public static final Comparator<Pair> SORT_BY_SUM_ASC = Comparator.comparingDouble(pair -> pair.sum);
public final int row;
public final double sum;
public Pair(int row, double sum) {
this.row = row;
this.sum = sum;
}
}
public static Pair findSum(double[][] arr, Comparator<Pair> comparator) {
return IntStream.range(0, arr.length)
.mapToObj(row -> new Pair(row, Arrays.stream(arr[row]).sum()))
.min(comparator).orElseThrow();
}
CodePudding user response:
Here is how you can find the max without using streams which I would think you can't do (since this may be for homework). This can also be applied to your findMinimum method. Rows start with 1.
What you can do is declare a record which is an immutable class which is perfect for situations like this.
record Result(int row, double sum){};
double[][] data = { { 12.0, 23.8, 44.2 }, { 45, 2, 3.8 },
{ 48.6, 22.5,17.9}, { 1, 2, 3, 4, 5, 6 } };
Result results = findMaximum(data);
System.out.printf("Maximum sum = %.2f in row %d%n", results.sum, results.row);
prints
Maximum sum = 89.00 in row 3
The method
public static Result findMaximum(double [][] arr) {
double maxSum = Double.MIN_VALUE;
int maxRow = 0;
for (int i = 0; i < arr.length; i ) {
double sum = 0;
double[] row = arr[i];
for (double item : row) {
sum = item;
}
if (maxSum < sum) {
maxSum = sum;
maxRow = i 1;
}
}
return new Result(maxRow, maxSum);
}
If you are allowed to use streams, this will work.
- stream indices of rows
- map the row and sum to a Result object
- find the maximum by check the sum of the just created object
- and return an optional.
Optional<Result> opt = IntStream.range(0, data.length)
.mapToObj(i -> new Result(i 1, Arrays.stream(data[i]).sum()))
.max(Comparator.comparing(Result::sum));
if (opt.isPresent()) {
Result results = opt.get();
System.out.printf("Maximum sum = %.2f in row %d%n", results.sum, results.row);
}
