fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main()
  6. {
  7. srand((unsigned int)time(NULL));
  8. int dimen, under_diag, main_diag, include_diag;
  9. under_diag = main_diag = include_diag = 0;
  10. dimen = 5;
  11. int r, c;
  12. int start_col, end_col, start_col_inc, end_col_inc;
  13. int max = 0;
  14. int min = 255;
  15.  
  16. printf("Enter matrix dimension: \n");
  17. scanf("%d", &dimen);
  18.  
  19. printf("Check over(0) or under(1)?: \n");
  20. scanf("%d", &under_diag);
  21.  
  22. printf("Check antidiagonal(0) or main diagonal(1)?: \n");
  23. scanf("%d", &main_diag);
  24.  
  25. printf("Exclude(0) or include(1) diagonal?: \n");
  26. scanf("%d", &include_diag);
  27.  
  28. unsigned char matrix[dimen][dimen];
  29. for(r = 0; r < dimen; ++r) {
  30. for(c = 0; c < dimen; ++c) {
  31. matrix[r][c] = rand() % 255;
  32. printf("%3d ", matrix[r][c]);
  33. }
  34. printf("\n");
  35. }
  36.  
  37. if(main_diag && under_diag) {
  38. start_col = 0;
  39. end_col = include_diag ? 0 : -1;
  40. start_col_inc = 0;
  41. end_col_inc = 1;
  42. } else if (main_diag && !under_diag) {
  43. start_col = include_diag ? 0 : 1;
  44. end_col = dimen - 1;
  45. start_col_inc = 1;
  46. end_col_inc = 0;
  47. } else if (!main_diag && under_diag) {
  48. start_col = include_diag ? dimen - 1 : dimen;
  49. end_col = dimen - 1;
  50. start_col_inc = -1;
  51. end_col_inc = 0;
  52. } else if (!main_diag && !under_diag) {
  53. start_col = 0;
  54. end_col = include_diag ? dimen - 1 : dimen - 2;
  55. start_col_inc = 0;
  56. end_col_inc = -1;
  57. }
  58.  
  59. for(r = 0; r < dimen;
  60. ++r, start_col += start_col_inc, end_col += end_col_inc) {
  61. for(c = start_col; c <= end_col; ++c) {
  62. if(matrix[r][c] > max) max = matrix[r][c];
  63. if(matrix[r][c] < min) min = matrix[r][c];
  64. }
  65. }
  66.  
  67. printf("Searched %s and %s the %s: Min - %d, Max - %d\n",
  68. under_diag ? "under" : "above",
  69. include_diag ? "including" : "not including",
  70. main_diag ? "main diagonal" : "antidiagonal",
  71. min, max);
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 2160KB
stdin
4
1
1
1
stdout
Enter matrix dimension: 
Check over(0) or under(1)?: 
Check antidiagonal(0) or main diagonal(1)?: 
Exclude(0) or include(1) diagonal?: 
 40  39 212  50 
215 124   0  34 
 90  80 218 160 
159 148  15 206 
Searched under and including the main diagonal: Min - 15, Max - 218