Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <ctime>
- using namespace std;
- typedef unsigned int index_t;
- int ** random(const size_t, const size_t);
- void show(int **, const size_t, const size_t);
- void freemem(int **, const size_t);
- long long summx(int *, const size_t);
- bool exists(int *, const size_t);
- int main() {
- setlocale(LC_CTYPE, ".1251");
- cout << " Введите M: "; size_t m; cin >> m;
- cout << " Введите N: "; size_t n; cin >> n;
- cout << endl;
- int ** matrix = NULL;
- if (matrix = random(m, n)) {
- show(matrix, m, n);
- long long max = LLONG_MIN, current;
- index_t index = m;
- for (index_t r = 0; r < m; r++) {
- if (exists(matrix[r], n)) {
- current = summx(matrix[r], n);
- if (current > max) {
- max = current;
- index = r;
- }
- }
- }
- if (index < m) {
- cout << " Наибольшая сумма в " << index + 1 << "-й строке." << endl
- << " Сумма равна: " << max << endl;
- } else cout << "\a Эксперитмент не удался!" << endl;
- freemem(matrix, m);
- matrix = NULL;
- }
- cin.get(); cin.get();
- return 0;
- }
- bool exists(int * _v, const size_t _n) {
- bool ex = false;
- if (_v && _n) ex = true;
- return ex;
- }
- long long summx(int * _v, const size_t _n) {
- long long sum = 0;
- for (index_t n = 0; n < _n; n++)
- sum += _v[n];
- return sum;
- }
- void freemem(int ** _mx, const size_t _m) {
- if (_mx && _m) {
- for (index_t m = 0; m < _m; m++)
- if (_mx[m])
- delete[] _mx[m];
- delete[] _mx;
- }
- }
- void show(int ** _mx, const size_t _m, const size_t _n) {
- if (_mx && _m) {
- for (index_t m = 0; m < _m; m++, cout << endl) {
- if (exists(_mx[m], _n)) {
- streamsize w = 3;
- for (index_t n = 0; n < _n; n++)
- cout << setw(w) << _mx[m][n];
- } else cout << "\a Строка не содержит данных!";
- }
- } else cout << "\a Матрица несуществует!";
- cout << endl;
- }
- int ** random(const size_t _m, const size_t _n) {
- int ** matrix = NULL;
- if (matrix = new (nothrow) int * [_m]) {
- srand(unsigned(time(NULL)));
- for (index_t m = 0; m < _m; m++) {
- if (matrix[m] = new (nothrow) int [_n]) {
- for (index_t n = 0; n < _n; n++)
- matrix[m][n] = 1 + rand() % 99;
- } else {
- for (index_t k = 0; k < m; k++)
- delete[] matrix[k];
- delete[] matrix;
- matrix = NULL;
- }
- if (!matrix) {
- cout << "\a Не удалось выделить память под матрицу!" << endl;
- break;
- }
- }
- }
- return matrix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement