Necto

Untitled

Jun 3rd, 2024
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int** fill() {
  5.     FILE* file = fopen("test.txt", "r");
  6.  
  7.     if (!file) {
  8.         printf("Error occurred while reading a file!");
  9.         return -1;
  10.     }
  11.  
  12.     int** arr = malloc(5 * sizeof(int*));
  13.     for (int i = 0; i < 5; ++i)
  14.         arr[i] = malloc(6 * sizeof(int));
  15.  
  16.     if (arr == NULL)
  17.         return -10;
  18.  
  19.     int number = 0;
  20.  
  21.     int i = 0, j = 0;
  22.  
  23.     while (fscanf(file, "%d", &number) > 0)
  24.     {
  25.         arr[j][i++] = number;
  26.         if (i == 6) {
  27.             ++j;
  28.             i = 0;
  29.         }
  30.     }
  31.  
  32.     fclose(file);
  33.     return arr;
  34. }
  35.  
  36. int main() {
  37.     int** arr = fill();
  38.  
  39.     int flag = 0;
  40.  
  41.     for (int i = 0; i < 5; ++i) {
  42.         int min = 9999999;
  43.         int min_arr[] = { 0, 0, 0, 0, 0, 0 };
  44.         for (int j = 0; j < 6; ++j) {
  45.             if (arr[i][j] < min) {
  46.                 min = arr[i][j];
  47.                 for (int l = 0; l < j; ++l)
  48.                     min_arr[l] = 0;
  49.  
  50.                 min_arr[j] = 1;
  51.             }
  52.  
  53.             else if (arr[i][j] == min)
  54.                 min_arr[j] = 1;
  55.         }
  56.         int max = -9999999;
  57.  
  58.         for (int n = 0; n < 6; ++n) {
  59.             if (min_arr[n] == 0)
  60.                 continue;
  61.             int max_idx = 0;
  62.             for (int k = 0; k < 5; ++k) {
  63.                 if (arr[k][n] > max) {
  64.                     max = arr[k][n];
  65.                     max_idx = k;
  66.                 }
  67.             }
  68.             if (max == arr[i][n]) {
  69.                 flag = 1;
  70.                 printf("%d %d \n", i, n);
  71.             }
  72.         }
  73.     }
  74.  
  75.     if (flag == 0)
  76.         printf("no such elements in given matrix");
  77.  
  78.     return 0;
  79. }
  80.  
  81.  
  82. 111 21 3 19 34 -252
  83. 29 82 92 20 101 123
  84. 1 -22 33 18 29 -231
  85. 21 923 75 -232 85 43
  86. -7 -5 4 3 -20 65
Advertisement
Add Comment
Please, Sign In to add comment