tags:

views:

264

answers:

2

I have an array of type int

matrix[][]

and it has values of 0 and 1

000000000
101010111
000010100
110011001

the values are different to the above, however this is a random example.

What i need to do is,

loop through the first row when column = 0 and row = 0 adding 1 to row to loop

if a one is found add it to a variable

when i get to the end of the row i need to then go down the colum 0 row = 0 adding 1 to column to get the loop

then i need to check the sum variable i have been adding to is % 2 = 0

then i need to check row 1 column 1

and repeat for all

the problerm i am having is determining when i have got to the end of a row, how is this calculated?

    for(int row = 0; row < matrix.length; row++){
        if(matrix[columns][row] == 1){
            sum ++;
            if(i am at the end of the row){
                //continue with other steps here
+5  A: 
for (int row = 0; row < matrix.length; row++) {
   int sum = 0;
   for (int col = 0; col < matrix[row].length; col++) {
      if (matrix[row][col] == 1){
        sum ++;
      }
   }
   // reached the end of the row
}
// reached the end of the array

So for each row (first line), you iterate over the columns in each row (the second line). This will cover all the elements in the 2d array. You know you've reached the end of the row since you've run out of columns (and exit the inner loop).

Brian Agnew
+3  A: 

Not really an exact answer to your question, but taking a step back (without knowing the context around your code) it looks to me you might want to use a BitSet instead of an array of 0's and 1's. It uses less memory and has a bunch of convenient methods (that are also usually faster than code you would write yourself). Counting the number of set bits which you seem to be doing for example is BitSet.cardinality().

Wouter Coekaerts