Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #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);
- bool __cdecl sum_array(int *, const size_t, int &);
- void __cdecl sum_row_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);
- sum_row_matrix(matrix, rows, cols);
- } else wcout << L"\aНеудалось заполнить матрицу!\n";
- delete_matrix(matrix, rows);
- matrix = NULL;
- cin.get(); cin.get();
- }
- inline void sum_row_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
- int sum;
- for (size_t n = 0; n < _rows; n++) {
- sum = 0;
- wcout << L"Сумма " << n + 1 << L"-ой строки: ";
- if (sum_array(_matrix[n], _cols, sum)) cout << sum << endl;
- }
- }
- bool sum_array(int * _array, const size_t _size, int & _sum) {
- double sum = 0;
- for (size_t n = 0; n < _size; n++) sum += _array[n];
- if (sum > INT_MAX || sum < INT_MIN) {
- wcout << L"Ошибка переполнения!\n";
- return false;
- } else _sum = int(sum);
- return true;
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement