Swap the elements between any two given quadrants of a Matrix | Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Given matrix ; Given quadrants ; Function Call
function swap ( N , M , mat , startx_X , starty_X , startx_Y , starty_Y ) { let row = 0 ; let col = 0 ; for ( let i = startx_X ; ; i ++ ) { col = 0 ; for ( let j = startx_X ; ; j ++ ) { let temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ] ; mat [ startx_Y + row ] [ starty_Y + col ] = temp ; col ++ ; if ( col >= M / 2 ) break ; } row ++ ; if ( row >= N / 2 ) break ; } } function swapQuadOfMatrix ( N , M , mat , X , Y ) { if ( X == 1 && Y == 2 ) { swap ( N , M , mat , 0 , 0 , 0 , M / 2 ) ; } else if ( X == 1 && Y == 3 ) { swap ( N , M , mat , 0 , 0 , N / 2 , 0 ) ; } else if ( X == 1 && Y == 4 ) { swap ( N , M , mat , 0 , 0 , N / 2 , M / 2 ) ; } else if ( X == 2 && Y == 3 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , 0 ) ; } else if ( X == 2 && Y == 4 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , M / 2 ) ; } else if ( X == 3 && Y == 4 ) { swap ( N , M , mat , N / 2 , 0 , N / 2 , M / 2 ) ; } prletMat ( N , M , mat ) ; } function prletMat ( N , M , mat ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let N = 6 , M = 6 ; let mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] , [ 19 , 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 , 30 ] , [ 31 , 32 , 33 , 34 , 35 , 36 ] ] ; let X = 1 , Y = 4 ; swapQuadOfMatrix ( N , M , mat , X , Y ) ;