Advertisement
avr39ripe

cppMoveLeftBADFixed

Sep 5th, 2021
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int horizon{ 4 };
  6.     const int vertical{ 4 };
  7.  
  8.  
  9.     int arr[vertical][horizon]{ {1, 15, 3, 4}, {5, 0, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 2} };
  10.  
  11.     // std::cout << "enter direction of shift 1-right, 2-left, 3-up, 4-down: ";
  12.     // std::cin >> dir;
  13.  
  14.     for (int y{ 0 }; y < vertical; ++y)
  15.     {
  16.         int copy;
  17.         for (int x{ 0 }; x < horizon; ++x)
  18.         {
  19.  
  20.             int shift_h{ 0 };
  21.             int shift_v{ 0 };
  22.  
  23.             /* if (arr[n][i+1] == 0)  // right
  24.              {
  25.                      shift_v = 0;
  26.                      shift_h = 1 ;
  27.  
  28.              }*/
  29.             if (arr[y][x - 1] == 0)  // left
  30.             {
  31.                 shift_h = -1;
  32.  
  33.                 copy = arr[y][x];
  34.                 arr[y][x] = 0;
  35.                 arr[y][x + shift_h] = copy;
  36.  
  37.                 ++x;
  38.             }
  39.  
  40.         }
  41.     }
  42.  
  43.  
  44.     for (int y{ 0 }; y < vertical; ++y)
  45.     {
  46.         for (int x{ 0 }; x < horizon; ++x)
  47.         {
  48.             std::cout << arr[y][x] << '\t';
  49.         }
  50.         std::cout << '\n';
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement