Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Shifting an array in C

  1. Aug 2, 2010 #1

    i would like some help with this task.

    My task is to write a function that shifts an array arbitrary number of steps.

    So for example the array { 0,1,2,3,4,5,6,7,8,9 }

    would look like { 9,0,1,2,3,4,5,6,7,8 } after the function was called with the steps arguments set to 1.

    The actual shifting isn't a problem, but rather how i deal with the last element in the list.

    For example if i shift all elements using a for loop
    Code (Text):

    int temp;
    for(int i=0; i<length_of_list; i++){
    temp = array[i+1];
    array[i+1] = array[i];
    array[i] = temp;
    On the last run i am left with temp, containing the last element in the list. How do i put it first?
  2. jcsd
  3. Aug 2, 2010 #2


    Staff: Mentor

    The array in your example has indexes that range form 0 through 9. If you were to shift all elements, say, three places higher in the array, where would each array element go? In particular, where would arr[7], arr[8], and arr[9] go? Can you think of an operator that might be helpful to you?
  4. Aug 2, 2010 #3

    Filip Larsen

    User Avatar
    Gold Member

    You may want to see if the modulus operator % can help you.

    Also, when moving stuff within the same array you have to be careful to iterate the "correct way" - as a rule of thumb you have to iterate the "opposite" way of the move. Your code above doesn't.
  5. Aug 2, 2010 #4
    Huh?, backwards?, why?
  6. Aug 3, 2010 #5

    Filip Larsen

    User Avatar
    Gold Member

    I can see that I in haste probably was confusing you more than helping with that comment.

    Normally when you move elements in an array, you move it by copying from one place to the other (that is, you don't swap the elements). In this case you must iterate the opposite way as I mentioned when moving within the same or overlapping array, or you will overwrite the array with a repeating pattern of the first elements you start moving (you can easily make a hand simulation to see this). In practice you will probably want to use the memmove function [1] or similar to move array contents and it handles the overlap issue just fine.

    However, in your case you have chosen to swap each element and then the direction of iteration does not matter. Using swap instead of move is indeed one way to solve the "direction problem", but you should know that it comes of the price of doing twice as many memory operations as is required when you do it with a move.

    In other words, while swapping logically is a perfectly valid solution I suspect that your instructor may still be interested to see you are able to handle the case of moving array elements.

    [1] http://www.cplusplus.com/reference/clibrary/cstring/memmove/
    Last edited by a moderator: Apr 25, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook