A Boolean Matrix Question | C # 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
using System ; class GFG { public static void modifyMatrix ( int [ , ] mat ) { bool row_flag = false ; bool col_flag = false ; for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; 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 ( int i = 1 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 1 ; j < mat . GetLength ( 1 ) ; j ++ ) { if ( mat [ 0 , j ] == 1 mat [ i , 0 ] == 1 ) { mat [ i , j ] = 1 ; } } } if ( row_flag == true ) { for ( int i = 0 ; i < mat . GetLength ( 1 ) ; i ++ ) { mat [ 0 , i ] = 1 ; } } if ( col_flag == true ) { for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { mat [ i , 0 ] = 1 ; } } } public static void printMatrix ( int [ , ] mat ) { for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } } ; Console . Write ( " Input ▁ Matrix ▁ : STRNEWLINE " ) ; printMatrix ( mat ) ; modifyMatrix ( mat ) ; Console . Write ( " Matrix ▁ After ▁ " + " Modification ▁ : STRNEWLINE " ) ; printMatrix ( mat ) ; } }