GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

counting consecutive ones in a binary matrix and storing them

Hello i am trying to do a bit maniipulation in a binary matrix. It can be either inputted manually or read from a file. One needs to count the number of consecutive 1s from each row of the binary matrix and then store them in a vector and output it in the console . Any help or step by step instruction on this would be appreciated.

asked Jul 4, 2016 by anonymous

1 Answer

public static void main(String[] args) {
        int searchingItem = 0;
        int dimensionX = 2, dimensionY = 4;
        int[][] a = new int[dimensionX][dimensionY];
        int[][] result = new int[dimensionX][dimensionY];
        
        //find
        for (int i = 0; i < 0; i++) {
            int row[] = a[i];
            boolean lastWasZero = false;
            for(int j = 0; j < 0; j++) {
                if(row[j] == searchingItem) {
                    if(lastWasZero) {
                        //do code of manipulation of adding to result of the next consecutive 0
                    } else {
                        //do code of manipulation of adding (or not adding? not clear from your tassk) to result the first found 0
                    }
                    lastWasZero = true;
                    
                } else {
                    lastWasZero = false;
                }
            }
        }
        //add cleaning of the result array?
        
        //print in console
        for (int i = 0; i < 0; i++) {
            for(int j = 0; j < 0; j++) {
                System.out.println(result[i][j] + " ");
            }
            System.out.println("");
        }
        
        //review code? and find optimisations of reducing dimensionality or smart algorithm improvement for increase in performance metrics?
    }

answered Jul 4, 2016 by guest
...