If I add the rows and columns together, I can identify which row and column
is wrong, but I can’t get my program to identify the position
exact.
A: Yes, the logic is to actually add each row and column, if the sum is greater than two(2) you will know the position of the row, column with more values or wherever there is more than 1’s, you should move to the position where the sum is less than two(2) this tells you exactly where is missing number 1, following your example the logic would be as follows:
For this matrix:
1 1 0 0
1 0 1 1
0 0 1 1
0 0 1 0
Let’s start by adding up all the lines first !
Add the first line of the Matrix :
1 1 0 0 ==> soma == 2 (OK)
Add the second row of the Matrix :
1 0 1 1 ==> soma == 3 (segunda linha da matriz com problema tem número a mais) > 2
Third line of the Matrix :
0 0 1 1 ==> soma == 2 (OK)
Fourth line of the Matrix:
0 0 1 0 ==> soma == 1 (quarta linha da matriz com problema tem número a menos) <2
Time to add up the columns:
First column:
1
1 ==> soma == 2 (OK)
0
0
Second column:
1
0 ==> soma == 1 (problema) < 2
0
0
Third column:
0
1 ==> soma == 3 (problema) > 2
1
1
Fourth column:
0
1 ==> soma == 2 (OK)
1
0
Now note which row and column has sum greater than two (>2), it will be exactly the second row and third column position (2,3)
you just find the positions with wrong values where if you have 1’s more than you should have, OK now to find for which position to move note which is the position whose value is less than two (<2) you will notice which will be the fourth row and second column (4,2)
, there is a logical way to get the result!
How’s your current code? My suggestion - assuming the number of 0s and 1s is correct - is to search for the row and column with 1s more, and move that position to the row and column with more 0s. If the row sum is correct but the column sum is not (or vice versa), move a
1
in the row itself, from the column with more to the column with less.– mgibsonbr