ChameL1oN

Лаба6_Двумерник

Dec 18th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <locale>
  3. #include <fstream>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. int m, n;
  9.  
  10. void print(int** matr){ // Печать на экран
  11. for (int i = 0; i < n; i++){
  12. for (int j = 0; j < m; j++){
  13. cout << matr[i][j] << " ";
  14. }
  15. cout << endl;
  16. }
  17. }
  18.  
  19. void otvorot_perevorot(int** &matr){ // Перевернуть все четные строки матрицы.
  20. for (int i = 1; i < n; i += 2){
  21. for (int j = 0; j < m/2; j++){
  22. int a = matr[i][j];
  23. matr[i][j] = matr[i][m - 1 - j];
  24. matr[i][m - 1 - j] = a;
  25. }
  26. }
  27. }
  28.  
  29. void del(int** &matr){ // Удалить строки, в которых отрицательных элементов больше, чем положительных
  30. int a, b;
  31. for (int i = n - 1; i>-1; i--){ //Обход матрицы с конца
  32. a = 0; // Отрицательные
  33. b = 0; // Положительные
  34. for (int j = 0; j < m; j++){ // Считаем кол-во отрицательных и положительных (0 считаем положительным)
  35. if (matr[i][j] < 0) a++;
  36. else b++;
  37. }
  38. if (a>b){ //Если условие, то удаляем
  39. for (int c = i; c < n-1; c++){
  40. for (int j = 0; j < m; j++){
  41. matr[c][j] = matr[c + 1][j];
  42. }
  43. }
  44. n--; // Уменшаем кол-во строк при удалении
  45. }
  46. }
  47. }
  48.  
  49.  
  50. void main(){
  51. srand(time(0)); // рандомизация генератора случайных чисел
  52. ifstream f("Text.txt", ios::in);
  53. cout << "Кол-во строк = ";
  54. cin >> n; //кол-во строк
  55. cout << "Кол-во столбцов = ";
  56. cin >> m; // кол-во столбцов
  57. int** matr = new int*[n];
  58. for (int i = 0; i < n; i++){
  59. matr[i] = new int[m];
  60. for (int j = 0; j < m; j++){
  61. matr[i][j] = rand()%20 - 10; //Рандом от -10 до 9
  62. }
  63. }
  64. print(matr);
  65. cout << endl;
  66. otvorot_perevorot(matr);
  67. print(matr);
  68. cout << endl;
  69. del(matr);
  70. print(matr);
  71. cout << endl;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment