Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- const int arrSize{ 10 };
- int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
- const int arrBegin{ 0 };
- const int arrEnd{ arrBegin + arrSize };
- const int rotatePos{ arrSize - 4 };
- for (int arrIdx{ arrBegin }; arrIdx != arrEnd; ++arrIdx) { std::cout << arr[arrIdx] << ' '; }std::cout << '\n';
- int first{arrBegin};
- int firstNew{ rotatePos };
- int last{arrEnd};
- int read{ firstNew };
- int write{ first };
- int readNext{ first }; // read position for when "read" hits "last"
- int copyElement;
- while (write != read and read != last)
- {
- while (read != last)
- {
- if (write == readNext)
- {
- readNext = read; // track where "first" went
- }
- copyElement = arr[read];
- arr[read] = arr[write];
- arr[write] = copyElement;
- ++read;
- ++write;
- }
- for (int arrIdx{ arrBegin }; arrIdx != arrEnd; ++arrIdx) { std::cout << arr[arrIdx] << ' '; }std::cout << '\n';
- read = readNext;
- }
- //for (int arrIdx{ arrBegin }; arrIdx != arrEnd; ++arrIdx) { std::cout << arr[arrIdx] << ' '; }std::cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement