35657

Untitled

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