Advertisement
therrontelford

Flipped Cell

Feb 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1.  
  2. public class FlippedCell {
  3.  
  4.     public static void main(String[] args) {
  5.         int j;
  6.         int[][] matrix = {
  7.                 {1,1,1,1,0,0},
  8.                 {0,0,1,1,1,1},
  9.                 {0,0,1,1,0,0},
  10.                 {1,1,0,0,1,1},
  11.                 {1,0,0,1,1,1},
  12.                 {1,0,1,0,1,1}
  13.         };
  14.         for (int i=0; i<matrix.length; i++) {
  15.             for (j=0; j<matrix[i].length; j++) {
  16.                 System.out.print(matrix[i][j]+" ");
  17.             }
  18.             System.out.println();
  19.         }
  20.         System.out.println();
  21.         int a=0;
  22.         int b=0;
  23.        
  24.         int x =(int)(Math.random()*6);
  25.         int y =(int)(Math.random()*6);
  26.         if (matrix[x][y]==1)
  27.             matrix[x][y]=0;
  28.         else
  29.             matrix[x][y] = 1;
  30.        
  31.         // find the row number that is not even 1's
  32.         for (int i=0; i<matrix.length; i++) {
  33.             int rowSum=0;
  34.             for( j=0; j<matrix[i].length; j++) {
  35.                 rowSum+=matrix[i][j];
  36.             }
  37.                 if (rowSum%2!=0) {
  38.                     a=i;
  39.                     b=j;// a column addition must occur
  40.                 }
  41.            
  42.         }
  43.             for (int i=0; i<matrix.length; i++) {
  44.                 for (j=0; j<matrix[i].length; j++) {
  45.                     System.out.print(matrix[i][j]+" ");
  46.                 }
  47.                 System.out.println();
  48.             }
  49.            
  50.             System.out.println("The flipped cell occurs at "+ a+", "+b);
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement