Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- int ** allocate_memory_matrix(int **, const size_t, const size_t);
- void destroy_matrix(int **, const size_t);
- int ** insert_column(int **, const size_t, rsize_t &, const size_t, int *, const size_t);
- void show_matrix(int **, const size_t, const size_t);
- void random_matrix(int **, const size_t, const size_t);
- void random_vector(int *, const size_t);
- void copy_matrix(int **, int **, const size_t, const size_t);
- int main() {
- locale::global(locale(""));
- srand(unsigned(time(NULL)));
- cout << " Введите исходные размеры матрицы: ";
- size_t rows, cols;
- cin >> rows >> cols;
- int ** matrix = NULL;
- if (matrix = allocate_memory_matrix(matrix, rows, cols)) {
- random_matrix(matrix, rows, cols);
- cout << "\n Исходная матрица:\n\n";
- show_matrix(matrix, rows, cols);
- int * vector = new int [rows];
- if (vector) {
- rsize_t attempt = 3;
- size_t index;
- while (attempt--) {
- random_vector(vector, rows);
- cout << " Введтите индекс вставки: ";
- cin >> index;
- matrix = insert_column(matrix, rows, cols, index, vector, rows);
- cout << "\n После вставки:\n\n";
- show_matrix(matrix, rows, cols);
- }
- delete[] vector;
- vector = NULL;
- }
- destroy_matrix(matrix, rows);
- }
- matrix = NULL;
- cout << "\n Тест окончен.\n";
- cin.get(); cin.get();
- return 0;
- }
- int ** allocate_memory_matrix(
- int ** _matrix,
- const size_t _rows,
- const size_t _cols
- )
- {
- _matrix = new int * [_rows];
- for (size_t n = 0; n < _rows; n++) _matrix[n] = new int [_cols];
- return _matrix;
- }
- void destroy_matrix(
- int ** _matrix,
- const size_t _rows
- )
- {
- for (size_t n = 0; n < _rows; n++) delete[] _matrix[n];
- delete[] _matrix;
- }
- int ** insert_column(
- int ** _matrix,
- const size_t _rows,
- rsize_t & _cols,
- const size_t _index,
- int * _vector,
- const size_t _size
- )
- {
- if (_matrix && _vector && _rows == _size && _index <= _cols) {
- int ** temp = NULL;
- if (temp = allocate_memory_matrix(temp, _rows, _cols)) {
- copy_matrix(temp, _matrix, _rows, _cols);
- destroy_matrix(_matrix, _rows);
- _matrix = NULL;
- size_t newsize = _cols + 1;
- _matrix = allocate_memory_matrix(temp, _rows, newsize);
- rsize_t m;
- for (rsize_t k = 0; k < _rows; k++) {
- m = 0;
- for (rsize_t n = 0; n < newsize; n++)
- if (n == _index) _matrix[k][n] = _vector[k];
- else _matrix[k][n] = temp[k][m++];
- }
- destroy_matrix(temp, _rows);
- temp = NULL;
- _cols = newsize;
- }
- }
- return _matrix;
- }
- void show_matrix(
- int ** _matrix,
- const size_t _rows,
- const size_t _cols
- )
- {
- for (rsize_t k = 0; k < _rows; k++, cout << endl)
- for (rsize_t n = 0; n < _cols; n++)
- cout << _matrix[k][n] << ' ';
- cout << endl;
- }
- void random_matrix(
- int ** _matrix,
- const size_t _rows,
- const size_t _cols
- )
- {
- for (rsize_t k = 0; k < _rows; k++)
- for (rsize_t n = 0; n < _cols; n++)
- _matrix[k][n] = rand() % 10;
- }
- void random_vector(
- int * _vector,
- const size_t _size
- )
- {
- for (rsize_t k = 0; k < _size; k++)
- _vector[k] = 10 + rand() % 90;
- }
- void copy_matrix(
- int **_buf,
- int ** _src,
- const size_t _rows,
- const size_t _cols
- )
- {
- for (rsize_t k = 0; k < _rows; k++)
- for (rsize_t n = 0; n < _cols; n++)
- _buf[k][n] = _src[k][n];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement