Home > Net >  How do i get minimum sum of matrix columns in Java?
How do i get minimum sum of matrix columns in Java?

Time:04-03

So i have this code, which calculates the sum of all columns in matrix and i have to print the smallest sum.

package com.example;

import java.util.Arrays;

public class discrete2 {

    public static void main(String[] args) {

        int a[][] = {
                {0, 1, 1, 0, 1, 0, 0, 1, 0},
                {1, 0, 1, 1, 0, 0, 0, 1, 0},
                {1, 0, 0, 1, 0, 1, 1, 0, 1},
                {0, 1, 0, 0, 0, 1, 0, 0, 1},
                {0, 0, 1, 1, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 1, 1, 0},
                {0, 0, 0, 0, 1, 1, 1, 0, 0}
        };

        int rows = a.length;
        int cols = a[0].length;

        int sumCol;

        for(int i = 0; i < cols; i  ){
            sumCol = 0;
            for(int j = 0; j < rows; j  ){
                sumCol = sumCol   a[j][i];
            }
            System.out.println("Sum of "   (i 1)  " column: "   sumCol);
        }
    }
}

I tried changing my code to have variable that remembers the smallest number but it's not working somehow and gives out 3, when it should be 2.

CodePudding user response:

public static void main(String[] args) {
    int a[][] = {
            {0, 1, 1, 0, 1, 0, 0, 1, 0},
            {1, 0, 1, 1, 0, 0, 0, 1, 0},
            {1, 0, 0, 1, 0, 1, 1, 0, 1},
            {0, 1, 0, 0, 0, 1, 0, 0, 1},
            {0, 0, 1, 1, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 1, 1, 0},
            {0, 0, 0, 0, 1, 1, 1, 0, 0}
    };

    int rows = a.length;
    int cols = a[0].length;

    int sumCol;
    int minSumCol = 100000000; // arbitrary large value

    for (int i = 0; i < cols; i  ) {
        sumCol = 0;
        for (int j = 0; j < rows; j  ) {
            sumCol = sumCol   a[j][i];
        }
        minSumCol = Math.min(minSumCol, sumCol);
        System.out.println("Sum of "   (i   1)   " column: "   sumCol);
    }
    System.out.println("Min sum in the matrix is: "   minSumCol);
}

Hi, your code is indeed correct. It does return 2 as the smallest value. This is the output that I got:

Sum of 1 column: 3

Sum of 2 column: 2

Sum of 3 column: 3

Sum of 4 column: 3

Sum of 5 column: 2

Sum of 6 column: 3

Sum of 7 column: 3

Sum of 8 column: 3

Sum of 9 column: 3

Min sum in the matrix is: 2

CodePudding user response:

You have to make little condition outside inner loop for find out the minimum sum of array. Here is down is condition.

if (sumCol < minRow && sumCol > 0){ 
     minRow = sumCol;
     minRowIndex = i   1;
}

Here down is modified code:

package com.example;
import java.util.Arrays;

public class discrete2 {

    public static void main(String[] args) {

        int a[][] = {
                {0, 1, 1, 0, 1, 0, 0, 1, 0},
                {1, 0, 1, 1, 0, 0, 0, 1, 0},
                {1, 0, 0, 1, 0, 1, 1, 0, 1},
                {0, 1, 0, 0, 0, 1, 0, 0, 1},
                {0, 0, 1, 1, 0, 0, 0, 0, 1},
                {1, 0, 0, 0, 0, 0, 1, 1, 0},
                {0, 0, 0, 0, 1, 1, 1, 0, 0}
        };

        int rows = a.length;
        int cols = a[0].length;

        int sumCol;
        int minRow = Integer.MAX_VALUE;
        int minRowIndex = 0;

        for(int i = 0; i < cols; i  ){
            sumCol = 0;
            for(int j = 0; j < rows; j  ){
                sumCol  = a[j][i];
            }
            System.out.println("Sum of Row "   (i   1)  " is: "   sumCol);
            // Find minimum sum of array
            if (sumCol < minRow && sumCol > 0){ 
                minRow = sumCol;
                minRowIndex = i   1;
            }
        }

        // Print minimum sum of array
        System.out.println("-------------------------------");
        System.out.println("Row "   minRowIndex   " has the minimum sum of "   minRow);
    }
}
  • Related