Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- int main()
- {
- //Инициализация рандома
- srand((unsigned)time(NULL));
- //Объявление и ввод переменных
- int rows, cols;
- cin >> rows >> cols;
- //Выделение памяти под дин. массив
- int** a = new int* [cols];
- for (int i = 0; i < cols; i++)
- a[i] = new int[rows];
- //Генерирование массива
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- a[i][j] = rand() % 31 - 5;
- }
- }
- //Вывод массива на экран
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- cout << a[i][j] << '\t';
- cout << endl;
- }
- cout << endl << endl;
- //Главный цикл алгоритма
- for (int j = 0; j < cols; j++)
- {
- //Проверка: положительный ли столбец
- bool isAllPositiive = true;
- for (int i = 0; i < rows && isAllPositiive; i++)
- if (a[i][j] < 0) isAllPositiive = false;
- //Если столбец положительный, то...
- if (isAllPositiive == true)
- {
- //То удаляем его, с помощью сдвига других столбцов
- cols = cols - 1;
- for (int j1 = j; j1 < cols; j1++)
- {
- for (int i = 0; i < rows; i++)
- {
- a[i][j1] = a[i][j1 + 1];
- }
- }
- }
- }
- //Вывод нового массива на экран
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- cout << a[i][j] << '\t';
- cout << endl;
- }
- //Очищение памяти
- for (int i = 0; i < rows; i++)
- delete[] a[i];
- delete[] a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement