Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- const int sizeY{ 5 };
- const int sizeX{ 5 };
- int arr2D[sizeY][sizeX]{
- {1,2,3,4,5},
- {6,7,8,9,10},
- {11,12,13,14,15},
- {16,17,18,19,20},
- {21,22,23,24,25}
- };
- int copyElement;
- char direction{ 'l' };
- int shifts{ 0 };
- int newHead{};
- while (true)
- {
- // output array
- for (int y{ 0 }; y < sizeY; ++y)
- {
- for (int x{ 0 }; x < sizeX; ++x)
- {
- std::cout << arr2D[y][x] << '\t';
- }
- std::cout << '\n';
- }
- std::cout << "Enter direction [l - left, r - right, u - up, d - down | x - exit]\n";
- std::cin >> direction;
- if (direction == 'x') { break; }
- std::cout << "Enter shifts count:\n";
- std::cin >> shifts;
- //fix shifts
- direction == 'l' or direction == 'r' ? shifts %= sizeX : shifts %= sizeY;
- newHead = shifts;
- // get newHead index
- if (direction == 'r') { newHead = sizeX - shifts; }
- else if (direction == 'd') { newHead = sizeY - shifts; }
- if (direction == 'l' or direction == 'r')
- {
- for (int y{ 0 }; y < sizeY; ++y)
- {
- int read{ newHead };
- int write{ 0 };
- int readNext{ 0 };
- int last{ sizeX };
- while (write != read and read != last)
- {
- while (read != last)
- {
- if (write == readNext)
- {
- readNext = read;
- }
- copyElement = arr2D[y][read];
- arr2D[y][read] = arr2D[y][write];
- arr2D[y][write] = copyElement;
- ++read;
- ++write;
- }
- read = readNext;
- }
- }
- }
- else if (direction == 'u' or direction == 'd')
- {
- int read{ newHead };
- int write{ 0 };
- int readNext{ 0 };
- int last{ sizeY };
- while (write != read and read != last)
- {
- while (read != last)
- {
- if (write == readNext)
- {
- readNext = read;
- }
- for (int x{ 0 }; x < sizeX; ++x)
- {
- copyElement = arr2D[read][x];
- arr2D[read][x] = arr2D[write][x];
- arr2D[write][x] = copyElement;
- }
- ++read;
- ++write;
- }
- read = readNext;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement