#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( )
{
int dimen, under_diag, main_diag, include_diag;
under_diag = main_diag = include_diag = 0 ;
dimen = 5 ;
int r, c;
int start_col, end_col, start_col_inc, end_col_inc;
int max = 0 ;
int min = 255 ;
printf ( "Enter matrix dimension: \n " ) ;
printf ( "Check over(0) or under(1)?: \n " ) ; scanf ( "%d" , & under_diag
) ;
printf ( "Check antidiagonal(0) or main diagonal(1)?: \n " ) ;
printf ( "Exclude(0) or include(1) diagonal?: \n " ) ; scanf ( "%d" , & include_diag
) ;
unsigned char matrix[ dimen] [ dimen] ;
for ( r = 0 ; r < dimen; ++ r) {
for ( c = 0 ; c < dimen; ++ c) {
matrix
[ r
] [ c
] = rand ( ) % 255 ; }
}
if ( main_diag && under_diag) {
start_col = 0 ;
end_col = include_diag ? 0 : - 1 ;
start_col_inc = 0 ;
end_col_inc = 1 ;
} else if ( main_diag && ! under_diag) {
start_col = include_diag ? 0 : 1 ;
end_col = dimen - 1 ;
start_col_inc = 1 ;
end_col_inc = 0 ;
} else if ( ! main_diag && under_diag) {
start_col = include_diag ? dimen - 1 : dimen;
end_col = dimen - 1 ;
start_col_inc = - 1 ;
end_col_inc = 0 ;
} else if ( ! main_diag && ! under_diag) {
start_col = 0 ;
end_col = include_diag ? dimen - 1 : dimen - 2 ;
start_col_inc = 0 ;
end_col_inc = - 1 ;
}
for ( r = 0 ; r < dimen;
++ r, start_col += start_col_inc, end_col += end_col_inc) {
for ( c = start_col; c <= end_col; ++ c) {
if ( matrix[ r] [ c] > max) max = matrix[ r] [ c] ;
if ( matrix[ r] [ c] < min) min = matrix[ r] [ c] ;
}
}
printf ( "Searched %s and %s the %s: Min - %d, Max - %d\n " , under_diag ? "under" : "above" ,
include_diag ? "including" : "not including" ,
main_diag ? "main diagonal" : "antidiagonal" ,
min, max) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRp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