35657

Untitled

Oct 1st, 2023
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip> // нужна для выравнивания вывода в консоль чисел с помощью setw
  3.  
  4. using namespace std;
  5.  
  6. void Print(int** arr, int lines, int cols) {
  7.     for (int i = 0; i < lines; i++) {
  8.         for (int j = 0; j < cols; j++) {
  9.             cout << setw(4) << arr[i][j];
  10.         }
  11.         cout << endl;
  12.     }
  13.     cout << endl;
  14. }
  15.  
  16. void Fill(int** arr, int lines, int cols) {
  17.     for (int i = 0; i < lines; i++) {
  18.         for (int j = 0; j < cols; j++) {
  19.             arr[i][j] = j + i * cols;
  20.         }
  21.     }
  22. }
  23.  
  24. int main() {
  25.  
  26.     setlocale(LC_ALL, "Russian");
  27.  
  28.     int lines = 5;
  29.     int cols = 5;
  30.  
  31.     int** arr = new int* [lines];
  32.  
  33.     for (int i = 0; i < lines; i++) {
  34.         arr[i] = new int[cols];
  35.     }
  36.  
  37.     Fill(arr, lines, cols);
  38.  
  39.     Print(arr, lines, cols);
  40.  
  41.     int index;
  42.  
  43.     cout << "Введите индекс: ";
  44.  
  45.     cin >> index;
  46.  
  47.     for (int i = 0; i < lines; ++i) {
  48.         int* temp = new int[cols + 1];
  49.         for (int j = 0; j < index; j++) {
  50.             temp[j] = arr[i][j];
  51.         }
  52.         temp[index] = 0;
  53.         for (int j = index; j < cols; j++) {
  54.             temp[j + 1] = arr[i][j];
  55.         }
  56.         delete[] arr[i];
  57.         arr[i] = temp;
  58.     }
  59.     cols++;
  60.  
  61.     Print(arr, lines, cols);
  62.  
  63.  
  64.     for (int i = 0; i < lines; i++) {
  65.         delete[] arr[i];
  66.     }
  67.     delete[] arr;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment