Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string>
- #include <iostream>
- #include <iomanip>
- #include <random>
- #include <ctime>
- using namespace std;
- class DinArr
- {
- public:
- int **arr;
- int *a;
- int col;
- int row;
- ////////////////
- DinArr()
- {
- col= 0;
- while (col<1 || col>20) //Разумные ограничения размера
- {
- cout << "Введите количество столбцов = ";
- cin >> col;
- }
- row= 0;
- while ( row<1 || row>20 )
- {
- cout << "Введите количество строк = ";
- cin >> row;
- }
- }
- /////////////////
- void MakeArr()
- {
- int i;
- arr = new int*[row]; if (arr==NULL) throw 10;
- for(i=0;i<row;i++)
- {
- a =new int[col];
- if (a==NULL) { Alarm= i; throw 11; }
- arr[i]= a;
- }
- Alarm= i;
- mt19937 gen ((int)time(0));
- uniform_int_distribution <int> distribution(-10,10);
- p= 0;
- for (int r= 0; r< row; r++)
- {
- for (int c= 0; c< col; c++)
- {
- arr[r][c] = distribution(gen);
- if (arr[r][c]>0) p++;
- }
- }
- }
- /////////////////
- void PrintArr()
- {
- int i= 0;
- for (int r= 0; r< row; r++)
- {
- for (int c= 0; c< col; c++)
- {
- cout << arr[r][c] << '\t';
- }
- cout << endl << endl;
- }
- cout << "Положительные элементы" << endl;
- for (int i= 0; i< p; i++)
- {
- cout << a[i] << '\t';
- }
- cout << endl;
- }
- /////////////////
- void MainFunc()
- {
- if (!p) throw 13;
- a = new int[p]; if (a==NULL) throw 12;
- int i= 0;
- for (int r= 0; r< row; r++)
- {
- for (int c= 0; c< col; c++)
- {
- if (arr[r][c]>0) { a[i]= arr[r][c]; i++; }
- }
- }
- }
- /////////////////
- ~DinArr()
- {
- cout << "Выход из программы и освобождаем память" << endl;
- for(int i=0;i<Alarm;i++) delete [] arr[i];
- delete [] arr;
- delete [] a;
- }
- protected:
- int p; //положительных элементов количество
- int Alarm;
- private:
- };
- int main(int argc, char **argv)
- {
- system("chcp 1251 > nul"); // Руссификация сообщений
- setlocale(LC_ALL, "Russian");
- DinArr da;
- try
- {
- da.MakeArr();
- cout << endl << endl;
- da.MainFunc(); da.PrintArr();
- }
- catch(int i)
- {
- switch (i)
- {
- case 10:
- cout << "Ошибка выделения памяти под строки..." << endl; break;
- case 11:
- cout << "Ошибка выделения памяти под столбцы..." << endl; break;
- case 12:
- cout << "Ошибка выделения памяти под положительные элементы..." << endl; break;
- case 13:
- cout << "Ошибка Положительные элементы отсутствуют..." << endl; break;
- }
- }
- system("pause"); // system("pause > nul");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement