Find smallest and largest element from square matrix diagonals | Function to find smallest and largest element from principal and secondary diagonal ; take length of $matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Declare and initialize 5 X5 matrix
< ? php function diagonalsMinMax ( $ mat ) { $ n = count ( $ mat ) ; if ( $ n == 0 ) return ; $ principalMin = $ mat [ 0 ] [ 0 ] ; $ principalMax = $ mat [ 0 ] [ 0 ] ; $ secondaryMin = $ mat [ $ n - 1 ] [ 0 ] ; $ secondaryMax = $ mat [ $ n - 1 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) { if ( $ mat [ $ i ] [ $ j ] < $ principalMin ) { $ principalMin = $ mat [ $ i ] [ $ j ] ; } if ( $ mat [ $ i ] [ $ j ] > $ principalMax ) { $ principalMax = $ mat [ $ i ] [ $ j ] ; } } if ( ( $ i + $ j ) == ( $ n - 1 ) ) { if ( $ mat [ $ i ] [ $ j ] < $ secondaryMin ) { $ secondaryMin = $ mat [ $ i ] [ $ j ] ; } if ( $ mat [ $ i ] [ $ j ] > $ secondaryMax ) { $ secondaryMax = $ mat [ $ i ] [ $ j ] ; } } } } echo " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , $ principalMin , " STRNEWLINE " ; echo " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " , $ principalMax , " STRNEWLINE " ; echo " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , $ secondaryMin , " STRNEWLINE " ; echo " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " , $ secondaryMax , " STRNEWLINE " ; } $ matrix = array ( array ( 1 , 2 , 3 , 4 , -10 ) , array ( 5 , 6 , 7 , 8 , 6 ) , array ( 1 , 2 , 11 , 3 , 4 ) , array ( 5 , 6 , 70 , 5 , 8 ) , array ( 4 , 9 , 7 , 1 , -5 ) ) ; diagonalsMinMax ( $ matrix ) ; ? >