35657

Untitled

Jan 25th, 2024
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void Fill(int** arr, int lines, int cols) {
  6.     for (int i = 0; i < lines; i++) {
  7.         for (int j = 0; j < cols; j++) {
  8.             arr[i][j] = rand() % 100;
  9.         }
  10.     }
  11. }
  12.  
  13. void Print(int** arr, int lines, int cols) {
  14.     for (int i = 0; i < lines; i++) {
  15.         for (int j = 0; j < cols; j++) {
  16.             cout << arr[i][j] << '\t';
  17.         }
  18.         cout << endl;
  19.     }
  20.     cout << endl;
  21. }
  22.  
  23. void AddColumn(int** arr, int lines, int& cols, int index) {
  24.     for (int i = 0; i < lines; i++) {
  25.         int* temp = new int[cols + 1];
  26.         for (int j = 0; j < index; j++) {
  27.             temp[j] = arr[i][j];
  28.         }
  29.         temp[index] = 0;
  30.         for (int j = index; j < cols; j++) {
  31.             temp[j + 1] = arr[i][j];
  32.         }
  33.         delete[] arr[i];
  34.         arr[i] = temp;
  35.     }
  36.     cols++;
  37. }
  38.  
  39. int main() {
  40.  
  41.     srand(time(NULL));
  42.  
  43.     int lines = 5, cols = 5;
  44.  
  45.     int** arr = new int* [lines];
  46.    
  47.     for (int i = 0; i < lines; i++) {
  48.         arr[i] = new int[cols];
  49.     }
  50.  
  51.     Fill(arr, lines, cols);
  52.  
  53.     Print(arr, lines, cols);
  54.  
  55.     AddColumn(arr, lines, cols, 3);
  56.  
  57.     Print(arr, lines, cols);
  58.  
  59.     for (int i = 0; i < lines; i++) {
  60.         delete[] arr[i];
  61.     }
  62.     delete[] arr;
  63.  
  64. }
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment