0

I currently need to add all the rows up in an array using only absolute values and then finding the maximum value out ot the three answers.

This is what I currently got:

public static int maxRowAbsSum(int[][] array) {
    int[][] maxRowValue = {

                            {3, -1,  4,  0},
                            {5,  9, -2,  6},
                            {5,  3,  7, -8}

                       };
    int maxRow = 0;
    int indexofMaxRow = 0;
        for (int column = 0; column < maxRowValue[0].length; column++) {
            maxRow += maxRowValue[0][column];
            }

            for (int row = 1; row < maxRowValue.length; row++) {
                int totalOfRow = 0;
                for (int column = 0; column < maxRowValue[row].length; column++){
                    totalOfRow += maxRowValue[row][column];

                    if (totalOfRow > maxRow) {
                        maxRow = totalOfRow;
                        indexofMaxRow = row;
                    }
                }

                System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow);
            }
            return indexofMaxRow;
        }

But this only returns the index row 1 total without using absolute values.

UPDATE: How would I write the JUnit code to test this using assertArrayEquals?

LtMuffin
  • 219
  • 2
  • 14
  • So, are you asking how to get the absolute value of an integer? Give a definition of absolute value. How could you possibly compute this value? – JB Nizet Apr 08 '18 at 15:44
  • The absolute value of integers I side the array, yes. – LtMuffin Apr 08 '18 at 15:45
  • So, what is the definition of absolute value? Is the definition different if the number is inside an array? – JB Nizet Apr 08 '18 at 15:47
  • `Math.abs(maxRowValue[row][column])` – c0der Apr 08 '18 at 15:48
  • Oh right. An absolute value is an integer without a negative value. E.g. 0 1 2 3 4 5 ...... So on. – LtMuffin Apr 08 '18 at 15:49
  • In this case, something should convert the negative integers to positive – LtMuffin Apr 08 '18 at 15:49
  • So, how could you do that? How about testing if the value is nagative? If it is, return the opposite of this value. if not, return this value? (Ignoring the fast that of course, Math.abs() does that for you: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Math.java#Math.abs%28int%29). – JB Nizet Apr 08 '18 at 15:58

2 Answers2

1

You should get the sum after visit all elements in the row, and it is redundant to calculate the first row separately:

int maxRow = 0;
int indexofMaxRow = 0;

for (int row = 0; row < maxRowValue.length; row++) {
    int totalOfRow = 0;
    for (int column = 0; column < maxRowValue[row].length; column++){
         if (maxRowValue[row][column] > 0) {
             totalOfRow += maxRowValue[row][column];
         } else {
             totalOfRow -= maxRowValue[row][column];
         }
     }
     if (totalOfRow > maxRow) {
         maxRow = totalOfRow;
         indexofMaxRow = row;
     }
}
System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow);
return indexofMaxRow;
xingbin
  • 27,410
  • 9
  • 53
  • 103
1

Your code is correct. Just a minor change.

1) Since you want absolute values, use Math.abs() function to add values.

2) The line: System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow); This displays the index of CURRENT MAXIMUM ROW. The index will change only if the value of the next row is greater. The sum of 3rd row is < 2nd row. Hence the index doesn't get updated in this case. So you get the same output in both the loops! This example would prove it: {3, -1, 4, 0}, {5, 9, -2, 6}, {5, 3, 7, 9} OUTPUT: Row 1 has the sum of 18 Row 2 has the sum of 24 2

        int[][] arr = {
            {3, -1,  4,  0},
            {5,  9, -2,  6},
            {5,  3,  7, -8}
       };

    int maxSum = 0;
    int i = 0;
    int tempSum = 0;

    for(int j=0; j<arr[0].length; j++) {
        //System.out.println("i: " + i + " j:" + j);
        tempSum += Math.abs(arr[i][j]);

        //if j is checking last element of the row, then go to next row, set tempSum to 0
        if(j==arr[0].length-1) {
            j=-1; i++; //j is set to -1 because it will get incremented to 0 because of for loop
            System.out.println("Sum of row: " + i + ": " + tempSum);
            if(tempSum > maxSum) maxSum = tempSum;
            tempSum=0;
        }

        //check if i completed all rows
        if(i == arr[0].length-1) {
            break;
        }
    }

    System.out.println("Max sum: " + maxSum);
d219
  • 2,707
  • 5
  • 31
  • 36
Gaurav Kumar
  • 136
  • 8
  • If you could add some information about what has been change in your code that solves the OP question that'd be great, cheers! – d219 Apr 08 '18 at 18:58
  • 1
    Your code is correct. Just a minor change. 1) Since you want absolute values, use Math.abs() function to add values. 2) The line: System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow); This displays the index of CURRENT MAXIMUM ROW. (the index will change only if the value of the next row is greater. The sum of 3rd row is < 2nd. Hence the index doesn't get updated in this case. So you get the same output in both the loops!) This example would prove it: {3, -1, 4, 0}, {5, 9, -2, 6}, {5, 3, 7, 9} OUTPUT: Row 1 has the sum of 18 Row 2 has the sum of 24 2 – Gaurav Kumar Apr 09 '18 at 20:18