A Boolean Matrix Question | PHP Code For A Boolean Matrix Question ; variables to check if there are any 1 in first row and column ; updating the first row and col if 1 is encountered ; Modify the input matrix mat [ ] using the first row and first column of Matrix mat ; modify first row if there was any 1 ; modify first col if there was any 1 ; A utility function to print a 2D matrix ; Driver Code
< ? php $ R = 3 ; $ C = 4 ; function modifyMatrix ( & $ mat ) { global $ R , $ C ; $ row_flag = false ; $ col_flag = false ; for ( $ i = 0 ; $ i < $ R ; $ i ++ ) { for ( $ j = 0 ; $ j < $ C ; $ j ++ ) { if ( $ i == 0 && $ mat [ $ i ] [ $ j ] == 1 ) $ row_flag = true ; if ( $ j == 0 && $ mat [ $ i ] [ $ j ] == 1 ) $ col_flag = true ; if ( $ mat [ $ i ] [ $ j ] == 1 ) { $ mat [ 0 ] [ $ j ] = 1 ; $ mat [ $ i ] [ 0 ] = 1 ; } } } for ( $ i = 1 ; $ i < $ R ; $ i ++ ) { for ( $ j = 1 ; $ j < $ C ; $ j ++ ) { if ( $ mat [ 0 ] [ $ j ] == 1 $ mat [ $ i ] [ 0 ] == 1 ) { $ mat [ $ i ] [ $ j ] = 1 ; } } } if ( $ row_flag == true ) { for ( $ i = 0 ; $ i < $ C ; $ i ++ ) { $ mat [ 0 ] [ $ i ] = 1 ; } } if ( $ col_flag == true ) { for ( $ i = 0 ; $ i < $ R ; $ i ++ ) { $ mat [ $ i ] [ 0 ] = 1 ; } } } function printMatrix ( & $ mat ) { global $ R , $ C ; for ( $ i = 0 ; $ i < $ R ; $ i ++ ) { for ( $ j = 0 ; $ j < $ C ; $ j ++ ) { echo $ mat [ $ i ] [ $ j ] . " " ; } echo " STRNEWLINE " ; } } $ mat = array ( array ( 1 , 0 , 0 , 1 ) , array ( 0 , 0 , 1 , 0 ) , array ( 0 , 0 , 0 , 0 ) ) ; echo " Input ▁ Matrix ▁ : STRNEWLINE " ; printMatrix ( $ mat ) ; modifyMatrix ( $ mat ) ; echo " Matrix ▁ After ▁ Modification ▁ : STRNEWLINE " ; printMatrix ( $ mat ) ; ? >