Advertisement
Guest User

Untitled

a guest
May 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. //Заполнение массива рандомными значениями
  8. void fillArray(int **arr, int Rows, int Cols)
  9. {
  10.     srand(time(0));
  11.     for (int i = 0; i<Rows; i++)
  12.         for (int j = 0; j < Cols; j++)
  13.             //Рандом значения от 0 до 2.
  14.             arr[i][j] = rand() % 3;
  15.  
  16. }
  17.  
  18. //Красивый вывод массива
  19. void printArray(int **arr, int Rows, int Cols)
  20. {
  21.     for (int i = 0; i<Rows; i++) {
  22.         for (int j = 0; j < Cols; j++)
  23.             cout << arr[i][j] << " ";
  24.         cout << endl;
  25.     }
  26. }
  27.  
  28. //Подсчёт числа нулевых элементов в столбце
  29. int checkNulls(int **arr, int Rows, int Cols, int *arrNulls, int &maxCol, int &countNulls)
  30. {
  31.     bool check = true;
  32.     int max = arrNulls[0];
  33.     for (int j = 0; j<Cols; j++) {
  34.         arrNulls[j] = 0;
  35.         for (int i = 0; i < Rows; i++) {
  36.             if (arr[i][j] == 0) {
  37.                 //Если элемент равен нулю, увеличим общее число нулей
  38.                 arrNulls[j] += 1;
  39.                 countNulls += 1;
  40.             }
  41.         }
  42.         cout << arrNulls[j] << " ";
  43.  
  44.         //Сравниваем, максимально ли нулей в этом столбце.
  45.         //Если да, запомнить в maxCol его позицию.
  46.         if (arrNulls[j]>max) {
  47.             maxCol = j;
  48.             max = arrNulls[j];
  49.         }
  50.     }
  51.  
  52.     for (int i = 0; i < Cols-1; i++)
  53.     {
  54.         if ((arrNulls[i] > arrNulls[i + 1]) and (arrNulls[i]!=0));
  55.         else check = false;
  56.     }
  57.  
  58.     return check;
  59. }
  60.  
  61. int main()
  62. {
  63.     setlocale(LC_ALL, "Russian");
  64.     int Rows = 5, Cols = 7,
  65.         n = 1, m = 0, maxCol = 0,
  66.         countNulls = 0,
  67.         **arr = new int*[Rows],
  68.         *arrNulls = new int[Cols];
  69.  
  70.     for (int i = 0; i < Rows; i++) {
  71.         arr[i] = new int[Cols];
  72.     }
  73.  
  74.  
  75.     fillArray(arr, Rows, Cols);
  76.     cout << "Массив:" << endl;
  77.     printArray(arr, Rows, Cols);
  78.  
  79.     cout << "\nНулей в каждом столбце:" << endl;
  80.     if (checkNulls(arr, Rows, Cols, arrNulls, maxCol, countNulls))
  81.         cout << "Всего нулей:"<< countNulls << endl;
  82.     else {
  83.         cout << "\nМаксимально нулей в столбце: " << maxCol+1 << endl;
  84.     }
  85.  
  86.     system("PAUSE");
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement