task3.cpp 744 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <iostream>
  2. void rotateArray(int* arr, int size, int shift) {
  3. shift = shift % size;
  4. if (shift < 0) {
  5. shift = (shift % size + size) % size;
  6. }
  7. int* temp = new int[size];
  8. for (int i = 0; i < size; ++i) {
  9. temp[(i + shift) % size] = arr[i];
  10. }
  11. for (int i = 0; i < size; ++i) {
  12. arr[i] = temp[i];
  13. }
  14. delete[] temp;
  15. }
  16. int main() {
  17. int arr[] = {1, 2, 3, 4, 5};
  18. int size = sizeof(arr) / sizeof(arr[0]);
  19. int shift = 2;
  20. rotateArray(arr, size, shift);
  21. std::cout << "{";
  22. for (int i = 0; i < size; ++i) {
  23. std::cout << arr[i];
  24. if (i < size - 1) {
  25. std::cout << ", ";
  26. }
  27. }
  28. std::cout << "}" << std::endl;
  29. return 0;
  30. }