Advertisement
NickAndNick

Матрица. Сумма элементов для каждой строки

Dec 16th, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. bool __cdecl random_matrix(int **, const size_t, const size_t, const int _left = 1, int _rigth = 1000);
  8. void __cdecl show_matrix(int **, const size_t, const size_t);
  9. void __cdecl delete_matrix(int **, size_t);
  10. int ** __cdecl create_matrix(int **, const size_t, const size_t);
  11. bool __cdecl sum_array(int *, const size_t, int &);
  12. void __cdecl sum_row_matrix(int **, const size_t, const size_t);
  13.  
  14. int main() {
  15.     srand(unsigned(time(NULL)));
  16.     wcout.imbue(locale("rus_rus.866"));
  17.  
  18.     wcout << L"Введите строки: ";
  19.     size_t rows;
  20.     cin >> rows;
  21.     wcout << L"Введите столбцы: ";
  22.     size_t cols;
  23.     cin >> cols;
  24.  
  25.     int ** matrix = NULL;
  26.     matrix = create_matrix(matrix, rows, cols);
  27.  
  28.     if (random_matrix(matrix, rows, cols)) {
  29.         wcout << L"Исходная матрица:\n";
  30.         show_matrix(matrix, rows, cols);
  31.         sum_row_matrix(matrix, rows, cols);    
  32.     } else wcout << L"\aНеудалось заполнить матрицу!\n";
  33.  
  34.     delete_matrix(matrix, rows);
  35.     matrix = NULL;
  36.  
  37.     cin.get(); cin.get();
  38. }
  39.  
  40. inline void sum_row_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  41.     int sum;
  42.     for (size_t n = 0; n < _rows; n++) {
  43.         sum = 0;
  44.         wcout << L"Сумма " << n + 1 << L"-ой строки: ";
  45.         if (sum_array(_matrix[n], _cols, sum)) cout << sum << endl;
  46.     }
  47. }
  48.  
  49. bool sum_array(int * _array, const size_t _size, int & _sum) {
  50.     double sum = 0;
  51.     for (size_t n = 0; n < _size; n++) sum += _array[n];
  52.     if (sum > INT_MAX || sum < INT_MIN) {
  53.         wcout << L"Ошибка переполнения!\n";
  54.         return false;
  55.     } else _sum = int(sum);
  56.     return true;
  57. }
  58.  
  59. inline bool random_matrix(int ** _matrix, const size_t _rows, const size_t _cols, const int _left, int _rigth) {
  60.     if (_left >= _rigth) return false;
  61.  
  62.     if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
  63.     else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
  64.     else if (_left >= 0 && _rigth > 0) _rigth -= _left;
  65.  
  66.     for (size_t row = 0; row < _rows; row++)
  67.         for (size_t col = 0; col < _cols; col++)
  68.             _matrix[row][col] = rand() % _rigth + _left;
  69.  
  70.     return true;
  71. }
  72.  
  73. void show_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  74.     if (_cols && _rows)
  75.         for (size_t row = 0; row < _rows; row++, cout << endl)
  76.             for (size_t col = 0; col < _cols; col++)
  77.                 cout << setw(5) << _matrix[row][col];
  78.     else wcout << L"Нет данных для отображения!\n";
  79. }
  80.  
  81. void delete_matrix(int ** _matrix, size_t _rows) {
  82.     for (size_t row = 0; row < _rows; row++)
  83.         delete[] _matrix[row];
  84.     delete[] _matrix;
  85. }
  86.  
  87. int ** create_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  88.     _matrix = new int * [_rows];
  89.  
  90.     for (size_t row = 0; row < _rows; row++)
  91.         _matrix[row] = new int [_cols];
  92.  
  93.     return _matrix;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement