Advertisement
NickAndNick

Матрица. Вставка столбцов

Jan 29th, 2013
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. int ** allocate_memory_matrix(int **, const size_t, const size_t);
  7. void destroy_matrix(int **, const size_t);
  8. int ** insert_column(int **, const size_t, rsize_t &, const size_t, int *, const size_t);
  9. void show_matrix(int **, const size_t, const size_t);
  10. void random_matrix(int **, const size_t, const size_t);
  11. void random_vector(int *, const size_t);
  12. void copy_matrix(int **, int **, const size_t, const size_t);
  13.  
  14. int main() {
  15.     locale::global(locale(""));
  16.     srand(unsigned(time(NULL)));
  17.  
  18.     cout << " Введите исходные размеры матрицы: ";
  19.     size_t rows, cols;
  20.     cin >> rows >> cols;
  21.  
  22.     int ** matrix = NULL;
  23.     if (matrix = allocate_memory_matrix(matrix, rows, cols)) {
  24.         random_matrix(matrix, rows, cols);
  25.         cout << "\n Исходная матрица:\n\n";
  26.         show_matrix(matrix, rows, cols);
  27.  
  28.         int * vector = new int [rows];
  29.         if (vector) {
  30.             rsize_t attempt = 3;
  31.             size_t index;
  32.  
  33.             while (attempt--) {
  34.                 random_vector(vector, rows);
  35.                 cout << " Введтите индекс вставки: ";
  36.                 cin >> index;
  37.                 matrix = insert_column(matrix, rows, cols, index, vector, rows);
  38.                 cout << "\n После вставки:\n\n";
  39.                 show_matrix(matrix, rows, cols);
  40.             }
  41.  
  42.             delete[] vector;
  43.             vector = NULL;
  44.         }
  45.         destroy_matrix(matrix, rows);
  46.     }
  47.  
  48.     matrix = NULL;
  49.     cout << "\n Тест окончен.\n";
  50.     cin.get(); cin.get();
  51.     return 0;
  52. }
  53.  
  54. int ** allocate_memory_matrix(
  55.     int ** _matrix,
  56.     const size_t _rows,
  57.     const size_t _cols
  58.     )
  59. {
  60.     _matrix = new int * [_rows];
  61.     for (size_t n = 0; n < _rows; n++) _matrix[n] = new int [_cols];
  62.     return _matrix;
  63. }
  64.  
  65. void destroy_matrix(
  66.     int ** _matrix,
  67.     const size_t _rows
  68.     )
  69. {
  70.     for (size_t n = 0; n < _rows; n++) delete[] _matrix[n];
  71.     delete[] _matrix;
  72. }
  73.  
  74. int ** insert_column(
  75.     int ** _matrix,
  76.     const size_t _rows,
  77.     rsize_t & _cols,
  78.     const size_t _index,
  79.     int * _vector,
  80.     const size_t _size
  81.     )
  82. {
  83.     if (_matrix && _vector && _rows == _size && _index <= _cols) {
  84.         int ** temp = NULL;
  85.  
  86.         if (temp = allocate_memory_matrix(temp, _rows, _cols)) {
  87.             copy_matrix(temp, _matrix, _rows, _cols);
  88.             destroy_matrix(_matrix, _rows);
  89.             _matrix = NULL;
  90.             size_t newsize = _cols + 1;
  91.             _matrix = allocate_memory_matrix(temp, _rows, newsize);
  92.             rsize_t m;
  93.  
  94.             for (rsize_t k = 0; k < _rows; k++) {
  95.                 m = 0;
  96.  
  97.                 for (rsize_t n = 0; n < newsize; n++)
  98.                     if (n == _index) _matrix[k][n] = _vector[k];
  99.                     else _matrix[k][n] = temp[k][m++];
  100.             }
  101.  
  102.             destroy_matrix(temp, _rows);
  103.             temp = NULL;
  104.  
  105.             _cols = newsize;
  106.         }
  107.     }
  108.     return _matrix;
  109. }
  110.  
  111. void show_matrix(
  112.     int ** _matrix,
  113.     const size_t _rows,
  114.     const size_t _cols
  115.     )
  116. {
  117.     for (rsize_t k = 0; k < _rows; k++, cout << endl)
  118.         for (rsize_t n = 0; n < _cols; n++)
  119.             cout << _matrix[k][n] << ' ';
  120.     cout << endl;
  121. }
  122.  
  123. void random_matrix(
  124.     int ** _matrix,
  125.     const size_t _rows,
  126.     const size_t _cols
  127.     )
  128. {
  129.     for (rsize_t k = 0; k < _rows; k++)
  130.         for (rsize_t n = 0; n < _cols; n++)
  131.             _matrix[k][n] = rand() % 10;
  132. }
  133.  
  134. void random_vector(
  135.     int * _vector,
  136.     const size_t _size
  137.     )
  138. {
  139.     for (rsize_t k = 0; k < _size; k++)
  140.         _vector[k] = 10 + rand() % 90;
  141. }
  142.  
  143. void copy_matrix(
  144.     int **_buf,
  145.     int ** _src,
  146.     const size_t _rows,
  147.     const size_t _cols
  148.     )
  149. {
  150.     for (rsize_t k = 0; k < _rows; k++)
  151.         for (rsize_t n = 0; n < _cols; n++)
  152.             _buf[k][n] = _src[k][n];
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement