Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //Инициализация рандома
  8. srand((unsigned)time(NULL));
  9.  
  10. //Объявление и ввод переменных
  11. int rows, cols;
  12. cin >> rows >> cols;
  13.  
  14. //Выделение памяти под дин. массив
  15. int** a = new int* [cols];
  16. for (int i = 0; i < cols; i++)
  17. a[i] = new int[rows];
  18.  
  19. //Генерирование массива
  20. for (int i = 0; i < rows; i++)
  21. {
  22. for (int j = 0; j < cols; j++)
  23. {
  24. a[i][j] = rand() % 31 - 5;
  25. }
  26. }
  27.  
  28. //Вывод массива на экран
  29. for (int i = 0; i < rows; i++)
  30. {
  31. for (int j = 0; j < cols; j++)
  32. cout << a[i][j] << '\t';
  33. cout << endl;
  34. }
  35. cout << endl << endl;
  36.  
  37. //Главный цикл алгоритма
  38. for (int j = 0; j < cols; j++)
  39. {
  40. //Проверка: положительный ли столбец
  41. bool isAllPositiive = true;
  42. for (int i = 0; i < rows && isAllPositiive; i++)
  43. if (a[i][j] < 0) isAllPositiive = false;
  44.  
  45. //Если столбец положительный, то...
  46. if (isAllPositiive == true)
  47. {
  48. //То удаляем его, с помощью сдвига других столбцов
  49. cols = cols - 1;
  50. for (int j1 = j; j1 < cols; j1++)
  51. {
  52. for (int i = 0; i < rows; i++)
  53. {
  54. a[i][j1] = a[i][j1 + 1];
  55. }
  56. }
  57. }
  58. }
  59.  
  60. //Вывод нового массива на экран
  61. for (int i = 0; i < rows; i++)
  62. {
  63. for (int j = 0; j < cols; j++)
  64. cout << a[i][j] << '\t';
  65. cout << endl;
  66. }
  67.  
  68. //Очищение памяти
  69. for (int i = 0; i < rows; i++)
  70. delete[] a[i];
  71. delete[] a;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement