Advertisement
Guest User

Untitled

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