Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <ctime>
- using namespace std;
- bool __cdecl random_matrix(int **, const size_t, const size_t, const int _left = -5, int _rigth = 51);
- void __cdecl show_matrix(int **, const size_t, const size_t);
- bool __cdecl is_positive_column(int **, const size_t, const size_t);
- void __cdecl delete_column(int **, int **, const size_t, const size_t, const size_t);
- void __cdecl copy_matrix(int **, int **, const size_t, const size_t);
- void __cdecl delete_matrix(int **, size_t);
- int ** __cdecl create_matrix(int **, const size_t, const size_t);
- int main() {
- srand(unsigned(time(NULL)));
- wcout.imbue(locale("rus_rus.866"));
- wcout << L"Введите строки: ";
- size_t rows;
- cin >> rows;
- wcout << L"Введите столбцы: ";
- size_t cols;
- cin >> cols;
- int ** matrix = NULL;
- matrix = create_matrix(matrix, rows, cols);
- if (random_matrix(matrix, rows, cols)) {
- wcout << L"Исходная матрица:\n";
- show_matrix(matrix, rows, cols);
- for (size_t index = 0; index < cols; index++)
- if (is_positive_column(matrix, rows, index)) {
- int ** temp = NULL;
- temp = create_matrix(temp, rows, cols);
- copy_matrix(temp, matrix, rows, cols);
- delete_matrix(matrix, rows);
- size_t new_cols = cols - 1;
- matrix = create_matrix(matrix, rows, new_cols);
- delete_column(matrix, temp, rows, cols, index);
- --cols;
- --index;
- delete_matrix(temp, rows);
- temp = NULL;
- }
- wcout << L"Изменённая матрица:\n";
- show_matrix(matrix, rows, cols);
- } else wcout << L"\aНе удалось заполнить матрицу!\n";
- delete_matrix(matrix, rows);
- matrix = NULL;
- cin.get(); cin.get();
- }
- inline bool random_matrix(int ** _matrix, const size_t _rows, const size_t _cols, const int _left, int _rigth) {
- if (_left >= _rigth) return false;
- if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
- else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
- else if (_left >= 0 && _rigth > 0) _rigth -= _left;
- for (size_t row = 0; row < _rows; row++)
- for (size_t col = 0; col < _cols; col++)
- _matrix[row][col] = rand() % _rigth + _left;
- return true;
- }
- void show_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
- if (_cols && _rows)
- for (size_t row = 0; row < _rows; row++, cout << endl)
- for (size_t col = 0; col < _cols; col++)
- cout << setw(5) << _matrix[row][col];
- else wcout << L"Нет данных для отображения!\n";
- }
- bool is_positive_column(int ** _matrix, const size_t _rows, const size_t _col_index) {
- for (size_t row = 0; row < _rows; row++)
- if (_matrix[row][_col_index] < 0)
- return false;
- return true;
- }
- void delete_column(int ** _matrix, int ** _temp, const size_t _rows, const size_t _cols, const size_t _col_index_del) {
- size_t next;
- for (size_t row = 0; row < _rows; row++) {
- next = 0;
- for (size_t col = 0; col < _cols; col++)
- if (col != _col_index_del)
- _matrix[row][next++] = _temp[row][col];
- }
- }
- void copy_matrix(int ** _in_matrix, int ** _from_matrix, const size_t _rows, const size_t _cols) {
- for (size_t row = 0; row < _rows; row++)
- for (size_t col = 0; col < _cols; col++)
- _in_matrix[row][col] = _from_matrix[row][col];
- }
- void delete_matrix(int ** _matrix, size_t _rows) {
- for (size_t row = 0; row < _rows; row++)
- delete[] _matrix[row];
- delete[] _matrix;
- }
- int ** create_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
- _matrix = new int * [_rows];
- for (size_t row = 0; row < _rows; row++)
- _matrix[row] = new int [_cols];
- return _matrix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement