Advertisement
NickAndNick

Обмен строк с максимальным и минимальными элементами

Feb 28th, 2013
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <Windows.h>
  5. #include <limits.h>
  6.  
  7. int main() {
  8.     int ** matrix = NULL;
  9.     int rows, cols;
  10.     int row, col;
  11.     int max, min;    
  12.     int max_index, min_index;
  13.     int temp;
  14.     int stop;
  15.  
  16.     system("chcp 1251");
  17.     system("cls");
  18.  
  19.     do {
  20.         printf(" Введите количество строк матрицы: ");
  21.         fflush(stdin);
  22.     } while(!scanf_s("%i", &rows) || rows < 1);
  23.  
  24.     do {
  25.         printf(" Введите количество столбцов матрицы: ");
  26.         fflush(stdin);
  27.     } while(!scanf_s("%i", &cols) || cols < 1);
  28.  
  29.     srand((unsigned int)time(NULL));
  30.  
  31.     if ((matrix = (int **)calloc(rows, sizeof(int *))) == NULL) {
  32.         printf(" Не удалось выделить память под массив указателей!\n\a");
  33.  
  34.         free(matrix);
  35.         matrix = NULL;
  36.  
  37.         printf(" Программа завершает свою работу!\n");
  38.         Sleep(3000);
  39.         exit(EXIT_FAILURE);
  40.     }
  41.  
  42.     for (row = 0; row < rows; row++) {
  43.         if ((matrix[row] = (int *)calloc(cols, sizeof(int ))) == NULL) {
  44.             printf(" Не удалось выделить память под массив элементов!\n\a");
  45.  
  46.             stop = row;
  47.  
  48.             for (row = 0; row < stop; row++) {
  49.                 free(matrix[row]);
  50.             }
  51.             free(matrix);
  52.             matrix = NULL;
  53.  
  54.             printf(" Программа завершает свою работу!\n");
  55.             Sleep(3000);
  56.             exit(EXIT_FAILURE);
  57.         }
  58.  
  59.         for (col = 0; col < cols; col++) {
  60.             matrix[row][col] = 1 + rand() % 999;
  61.         }
  62.     }
  63.  
  64.     printf("\n");
  65.  
  66.     max = INT_MIN, min = INT_MAX;
  67.     max_index = min_index = 0;
  68.  
  69.     for (row = 0; row < rows; row++, printf("\n")) {
  70.         for (col = 0; col < cols; col++) {
  71.             printf("%5i", matrix[row][col]);
  72.  
  73.             if (max < matrix[row][col]) {
  74.                 max = matrix[row][col];
  75.                 max_index = row;
  76.             }
  77.  
  78.             if (min > matrix[row][col]) {
  79.                 min = matrix[row][col];
  80.                 min_index = row;
  81.             }
  82.         }
  83.     }
  84.  
  85.     if (min_index != max_index) {
  86.         for (col = 0; col < cols; col++) {
  87.             temp = matrix[min_index][col];
  88.             matrix[min_index][col] = matrix[max_index][col];
  89.             matrix[max_index][col] = temp;
  90.         }
  91.  
  92.         printf("\n\n");
  93.  
  94.         for (row = 0; row < rows; row++, printf("\n")) {
  95.             for (col = 0; col < cols; col++) {
  96.                 printf("%5i", matrix[row][col]);
  97.             }
  98.         }
  99.     } else {
  100.         printf("\n Максимальный и минимальный элементы находятся в одной строке!\n");
  101.     }
  102.  
  103.     for (row = 0; row < rows; row++) {
  104.         free(matrix[row]);
  105.     }
  106.     free(matrix);
  107.     matrix = NULL;
  108.  
  109.     fflush(stdin);
  110.     getchar();
  111.  
  112.     return EXIT_SUCCESS;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement