-2

I don't post the code because I don't know how to write it, I have an array of integers and a number 'k' entered by the user. What the program has to do is translate the elements of the array k positions forward. I don't know how to do when the last elements come out of the array while they should be positioned between the first cells. A sort of circular movement of values. Which from the end return to the beginning of the array.

Francesco
  • 111
  • 1
  • 7
  • 3
    Does this answer your question? [How do you implement a circular buffer in C?](https://stackoverflow.com/questions/827691/how-do-you-implement-a-circular-buffer-in-c) – user14063792468 Dec 25 '20 at 08:31

1 Answers1

0

If you're at index i in an array, (i + k) mod array_length will give you circular motion. For example, moving two spots forward in the middle of a 3-length array:

[a b c] 
   ^ i = 1

[a b c] ?
        ^ i = (1 + 2) mod 3

[a b c]
 ^ i = 3 mod 3 = 0

Depending on how you implement your solution you may want to check if k and array_length are coprime. If they aren't you could end up going around the array on the same loop and not cover every element.

Drake
  • 53
  • 4