I have this code, which calculates the sum of all rows in 2D array but I want to print the smallest sum of rows also.
Here down is my 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;
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 to 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.