Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "iostream"
- #include <ctime>
- //Библиотека для функции swap();
- #include <iomanip>
- using namespace std;
- //Заполнение массива рандомными значениями
- void fillArray(int **arr, int Rows, int Cols)
- {
- srand(time(0));
- for (int i = 0; i<Rows; i++)
- for (int j = 0; j < Cols; j++)
- //Рандом значения от 0 до 2.
- arr[i][j] = rand() % 3;
- }
- //Красивый вывод массива
- void printArray(int **arr, int Rows, int Cols)
- {
- for (int i = 0; i<Rows; i++) {
- for (int j = 0; j < Cols; j++)
- cout << arr[i][j] << " ";
- cout << endl;
- }
- }
- //Подсчёт числа нулевых элементов в столбце
- int checkNulls(int **arr, int Rows, int Cols, int *arrNulls, int &maxCol, int &countNulls)
- {
- bool check = true;
- int max = arrNulls[0];
- for (int j = 0; j<Cols; j++) {
- arrNulls[j] = 0;
- for (int i = 0; i < Rows; i++) {
- if (arr[i][j] == 0) {
- //Если элемент равен нулю, увеличим общее число нулей
- arrNulls[j] += 1;
- countNulls += 1;
- }
- }
- cout << arrNulls[j] << " ";
- //Сравниваем, максимально ли нулей в этом столбце.
- //Если да, запомнить в maxCol его позицию.
- if (arrNulls[j]>max) {
- maxCol = j;
- max = arrNulls[j];
- }
- }
- for (int i = 0; i < Cols-1; i++)
- {
- if ((arrNulls[i] > arrNulls[i + 1]) and (arrNulls[i]!=0));
- else check = false;
- }
- return check;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int Rows = 5, Cols = 7,
- n = 1, m = 0, maxCol = 0,
- countNulls = 0,
- **arr = new int*[Rows],
- *arrNulls = new int[Cols];
- for (int i = 0; i < Rows; i++) {
- arr[i] = new int[Cols];
- }
- fillArray(arr, Rows, Cols);
- cout << "Массив:" << endl;
- printArray(arr, Rows, Cols);
- cout << "\nНулей в каждом столбце:" << endl;
- if (checkNulls(arr, Rows, Cols, arrNulls, maxCol, countNulls))
- cout << "Всего нулей:"<< countNulls << endl;
- else {
- cout << "\nМаксимально нулей в столбце: " << maxCol+1 << endl;
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement