Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Prlet the matrix ; Driver Code
function generateMatrix ( N ) { let odd = 1 ; let even = 2 ; let mat = new Array ( N + 1 ) ; for ( var i = 0 ; i < mat . length ; i ++ ) { mat [ i ] = new Array ( 2 ) ; } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { mat [ i ] [ j ] = odd ; odd += 2 ; } else { mat [ i ] [ j ] = even ; even += 2 ; } } } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let N = 4 ; generateMatrix ( N ) ;