Thread: shifting array elements

  1. #1
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655

    shifting array elements

    is there a way to shift the elements in an array from this...
    Code:
    char array[5] = {1,2,3,4,5}
    to this...?
    Code:
    array[5] = {3,4,5,1,2}
    im trying to study for a test next week and i thought i remember something like this on the last test....only i cant find it to look over
    guns dont kill people, abortion clinics kill people.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    you're moving each element in the array twice forward, when you reach the end, the next forward step will result to the number to get in the first place.

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    so youre saying just shift the array left 2 and it will automatically put the other numbers at the end of the array?
    guns dont kill people, abortion clinics kill people.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dP munky
    so youre saying just shift the array left 2 and it will automatically put the other numbers at the end of the array?
    No. There is no way to "shift an array". You have to do it manually. (Technicly, I suppose you could use memcopy or something, but you'd still have to manually wrap the others around).

    You should be able to do it with three memcopy lines.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    >>You should be able to do it with three memcopy lines

    would it just be easier to copy it into a whole new array, if i know the array has this, this, and this element, just copy those elements into a new array....shifted 2 spaces
    guns dont kill people, abortion clinics kill people.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dP munky
    >>You should be able to do it with three memcopy lines

    would it just be easier to copy it into a whole new array, if i know the array has this, this, and this element, just copy those elements into a new array....shifted 2 spaces
    Define easier.

    Given another array to hold what you need to wrap, you just:

    Right shift, say 3 spaces:
    a) abcdefg <-- put that into a temp array
    b) abcdefg <-- write that over the front of this array.
    c) abc <-- write that over the end

    Do the opposite for a left shift.
    Three lines.

    It all depends on what you consider "easier".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  2. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 08:05 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM