Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <locale>
- #include <fstream>
- #include <time.h>
- using namespace std;
- int m, n;
- void print(int** matr){ // Печать на экран
- for (int i = 0; i < n; i++){
- for (int j = 0; j < m; j++){
- cout << matr[i][j] << " ";
- }
- cout << endl;
- }
- }
- void otvorot_perevorot(int** &matr){ // Перевернуть все четные строки матрицы.
- for (int i = 1; i < n; i += 2){
- for (int j = 0; j < m/2; j++){
- int a = matr[i][j];
- matr[i][j] = matr[i][m - 1 - j];
- matr[i][m - 1 - j] = a;
- }
- }
- }
- void del(int** &matr){ // Удалить строки, в которых отрицательных элементов больше, чем положительных
- int a, b;
- for (int i = n - 1; i>-1; i--){ //Обход матрицы с конца
- a = 0; // Отрицательные
- b = 0; // Положительные
- for (int j = 0; j < m; j++){ // Считаем кол-во отрицательных и положительных (0 считаем положительным)
- if (matr[i][j] < 0) a++;
- else b++;
- }
- if (a>b){ //Если условие, то удаляем
- for (int c = i; c < n-1; c++){
- for (int j = 0; j < m; j++){
- matr[c][j] = matr[c + 1][j];
- }
- }
- n--; // Уменшаем кол-во строк при удалении
- }
- }
- }
- void main(){
- srand(time(0)); // рандомизация генератора случайных чисел
- ifstream f("Text.txt", ios::in);
- cout << "Кол-во строк = ";
- cin >> n; //кол-во строк
- cout << "Кол-во столбцов = ";
- cin >> m; // кол-во столбцов
- int** matr = new int*[n];
- for (int i = 0; i < n; i++){
- matr[i] = new int[m];
- for (int j = 0; j < m; j++){
- matr[i][j] = rand()%20 - 10; //Рандом от -10 до 9
- }
- }
- print(matr);
- cout << endl;
- otvorot_perevorot(matr);
- print(matr);
- cout << endl;
- del(matr);
- print(matr);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment