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 = 1, int _rigth = 1000);
- void __cdecl show_matrix(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);
- void __cdecl quick_sort(int *, const size_t, const size_t);
- void __cdecl uswap(int &, int &);
- 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 row = 1; row < rows; row += 2)
- quick_sort(matrix[row], 0, cols - 1);
- wcout << L"Изменённая матрица:\n";
- show_matrix(matrix, rows, cols);
- } else wcout << L"\aНе удалось заполнить матрицу!\n";
- delete_matrix(matrix, rows);
- matrix = NULL;
- cin.get(); cin.get();
- return 0;
- }
- 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";
- }
- 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;
- }
- void quick_sort(int * _array, const size_t _beg, const size_t _end) {
- int n = (int)_beg, m = (int)_end;
- int mid = _array[(_beg + _end) >> 1];
- do {
- while (_array[n] < mid && n < (int)_end) ++n;
- while (_array[m] > mid && m > (int)_beg) --m;
- if (n <= m) {
- if (n < m) uswap(_array[n], _array[m]);
- ++n;
- --m;
- }
- } while (n <= m);
- if ((int)_beg < m) quick_sort(_array, _beg, m);
- if (n < (int)_end) quick_sort(_array, n, _end);
- }
- void uswap(int & _a, int & _b) {
- int temp = _a;
- _a = _b;
- _b = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement