Advertisement
avr39-ripe

deleteLastLineIn2dDynArr

Apr 13th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     int ySize{ 10 };
  6.     int xSize{ 10 };
  7.  
  8.     int** arr2d { new int* [ySize] };
  9.  
  10.     for (int y{ 0 }; y < ySize; ++y)
  11.     {
  12.         arr2d[y] = new int[xSize];
  13.     }
  14.  
  15.     for (int y{ 0 }; y < ySize; ++y)
  16.     {
  17.         for (int x{ 0 }; x < xSize; ++x)
  18.         {
  19.             arr2d[y][x] = rand() % 10;
  20.         }
  21.     }
  22.  
  23.     for (int y{ 0 }; y < ySize; ++y)
  24.     {
  25.         for (int x{ 0 }; x < xSize; ++x)
  26.         {
  27.             std::cout << arr2d[y][x] << ' ';
  28.         }
  29.         std::cout << '\n';
  30.     }
  31.     std::cout << '\n';
  32.  
  33.     delete[] arr2d[ySize - 1];
  34.     int** tmp = new int * [--ySize];
  35.     for (int y{ 0 }; y < ySize; ++y)
  36.     {
  37.         tmp[y] = arr2d[y];
  38.     }
  39.     delete[] arr2d;
  40.     arr2d = tmp;
  41.  
  42.     std::cout << "New ySize is: " << ySize << "\n\n";
  43.  
  44.     for (int y{ 0 }; y < ySize; ++y)
  45.     {
  46.         for (int x{ 0 }; x < xSize; ++x)
  47.         {
  48.             std::cout << arr2d[y][x] << ' ';
  49.         }
  50.         std::cout << '\n';
  51.     }
  52.     std::cout << '\n';
  53.  
  54.     for (int y{ 0 }; y < ySize; ++y)
  55.     {
  56.         delete[] arr2d[y];
  57.     }
  58.     delete[] arr2d;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement