1

As the title says, I want to know a way (in Java) to find which row (in a matrix/2D Array) and column has the highest sum of its numbers. There might be an easy solution but I'm struggling to find it.

I currently have the first part of the program but I can't seem to find a solution to the second part, which is finding the row and column with the highest sum.

Desired output

I'm a beginner at this so any kind of advice would be appreciated.

This is the first part of my code:

import javax.swing.JOptionPane;

public class summat{
    public static void main(String[] args){
        int mat[][] = new int [3][3];
        int num, sumop, sumw, i, j, mayop = 0, mayw = 0;

        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                String input = JOptionPane.showInputDialog(null, "Products sold by the operator " +  (i+1) + " in week " + (j+1) + ".");
                mat[i][j] = Integer.parseInt(input);
            }
        }

        /*Sum of individual rows*/
        for(i=0;i<3;i++){
            sumop = 0;
            for(j=0;j<3;j++){
                sumop = sumop + mat[i][j];
            }
            JOptionPane.showMessageDialog(null, "The operator " + (i+1) + " sold " + sumop + " units.");
        }

        /*Sum of individual columns*/
        for(j=0;j<3;j++){
            sumw = 0;
            for(i=0;i<3;i++){
                sumw = sumw + mat[i][j];
            }
            JOptionPane.showMessageDialog(null, "In week " + (j+1) + " the company sold " + sumw + " units.");
        }

    }
}
Mauricio
  • 13
  • 1
  • 7

4 Answers4

1
public static void method(int[] arr, int row, int col) {
    // converting array to matrix
    int index = 0;
    int mat[][] = new int[row][col];
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            mat[i][j] = arr[index];
            index++;
        }
    }
    // calculating sum of each row and adding to arraylist
    ArrayList<Integer> rsum = new ArrayList<Integer>();
    for (int i = 0; i < row; i++) {
        int r = 0;
            for (int j = 0; j < col; j++) {
            r = r + mat[i][j];
            }
        rsum.add(r);
    }
    // calculating sum of each col and adding to arraylist
    ArrayList<Integer> csum = new ArrayList<Integer>();
    for (int i = 0; i < row; i++) {
        int sum = 0;
        for (int j = 0; j < col; j++) {
            sum = sum + mat[j][i];
            }
        csum.add(sum);
        }
    System.out.println(
            "Maximum row sum is " + Collections.max(rsum) + " at row " + rsum.indexOf(Collections.max(rsum)));
    System.out.println(
            "Maximum col sum is " + Collections.max(csum) + " at col " + csum.indexOf(Collections.max(csum)));   
}

public static void method(int[][] mat, int row, int col) {

    // calculating sum of each row and adding to arraylist
    ArrayList<Integer> rsum = new ArrayList<Integer>();
    for (int i = 0; i < row; i++) {
        int r = 0;
        for (int j = 0; j < col; j++) {
            r = r + mat[i][j];
        }
        rsum.add(r);
    }
    // calculating sum of each col and adding to arraylist
    ArrayList<Integer> csum = new ArrayList<Integer>();
    for (int i = 0; i < row; i++) {
        int sum = 0;
        for (int j = 0; j < col; j++) {
            sum = sum + mat[j][i];
        }
        csum.add(sum);
    }
    System.out.println(
            "Maximum row sum is " + Collections.max(rsum) + " at row " + rsum.indexOf(Collections.max(rsum)));
    System.out.println(
            "Maximum col sum is " + Collections.max(csum) + " at col " + csum.indexOf(Collections.max(csum)));
}
vh1ne
  • 287
  • 2
  • 10
  • Here you can find the answer. I added 2 methods one with 1d array as input another with 2 d array. row column start at 0 index you can add 1 for convinience – vh1ne Feb 21 '20 at 09:54
0

You can have to integers one for row(maxRow) and one for col(maxCol) to maintain max values:

    int maxRow = Integer.MIN_VALUE;
    /*Sum of individual rows*/
    for(i=0;i<3;i++){
        sumop = 0;
        for(j=0;j<3;j++){
            sumop = sumop + mat[i][j];
        }
        if(maxRow > sumop)
            maxRow = sumop;
        JOptionPane.showMessageDialog(null, "The operator " + (i+1) + " sold " + sumop + " units.");
    }

    int maxCol = Integer.MIN_VALUE;
    /*Sum of individual columns*/
    for(j=0;j<3;j++){
        sumw = 0;
        for(i=0;i<3;i++){
            sumw = sumw + mat[i][j];
        }
        if(maxCol > sumw)
            maxCol = sumw;
        JOptionPane.showMessageDialog(null, "In week " + (j+1) + " the company sold " + sumw + " units.");
    }
AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16
0

You can use the following logic and implement it as desired.

    // Row calculation
    int rowSum = 0, maxRowSum = Integer.MIN_VALUE, maxRowIndex = Integer.MIN_VALUE;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            rowSum = rowSum + mat[i][j];
        }
        if (maxRowSum < rowSum) {
            maxRowSum = rowSum;
            maxRowIndex = i;
        }
        rowSum = 0;   // resetting before next iteration
    }

    // Column calculation
    int colSum = 0, maxColSum =  Integer.MIN_VALUE, maxColIndex = Integer.MIN_VALUE;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            colSum = colSum + mat[j][i];
        }
        if (maxColSum < colSum) {
            maxColSum = colSum;
            maxColIndex = i; 
        }
        colSum = 0;    // resetting before next iteration
    }

    System.out.println("Row " + maxRowIndex + " has highest sum = " +maxRowSum);
    System.out.println("Col " + maxColIndex + " has highest sum = " +maxColSum);

Here we use two additional variables maxRowSum to store the highest sum of the row and maxRowIndex to store the index of the highest row. The same applies for column as well.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
0

Here's a method that first computes both row-wise and column-wise sums in one loop (the same one it uses to find max row sum), and a second one to find the max column sum:

//This returns an array with {maxRowIndex, maxColumnIndex}
public static int[] findMax(int[][] mat) {
    int[] rowSums = new int[mat.length];
    int[] colSums = new int[mat[0].length];

    int maxRowValue = Integer.MIN_VALUE;
    int maxRowIndex = -1;

    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat[i].length; j++) {
            rowSums[i] += mat[i][j];
            colSums[j] += mat[i][j];
        }

        if (rowSums[i] > maxRowValue) {
            maxRowIndex = i;
            maxRowValue = rowSums[i];
        }

        // display current row message
        JOptionPane.showMessageDialog(null, "The operator " +
                (i + 1) + " sold " + rowSums[i] + " units.");
    }

    int maxColumnValue = Integer.MIN_VALUE;
    int maxColumnIndex = -1;

    // look for max column:
    for (int j = 0; j < mat[0].length; j++) {
        if (colSums[j] > maxColumnValue) {
            maxColumnValue = colSums[j];
            maxColumnIndex = j;
        }

        // display column message
        JOptionPane.showMessageDialog(null, "In week " + 
        (j + 1) + " the company sold " + colSums[j] + " units.");
    }

    return new int[] { maxRowIndex, maxColumnIndex };
}

The following test (I had to hard-code matrix values) produces [2, 2]:

public static void main(String[] args) {
    int mat[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    int[] maxValues = findMax(mat);
    System.out.println("Max row index: " + 
       maxValues[0] + ". Max Column index: " + maxValues[1]);
}
ernest_k
  • 44,416
  • 5
  • 53
  • 99