Thread: Shifting elements in an Array

  1. #1
    Registered User mmarab's Avatar
    Join Date
    Jul 2007
    Posts
    30

    Talking Shifting elements in an Array

    Hi,

    Was wondering if anyone could help, basically i want to shift some elements in an array when new ones are added.

    For Example:

    My Array contains: <1,2,3,4,5>
    Then i add the following elements: 6 and 7
    So i would like my array to end up looking like this: <3,4,5,6,7>

    So basically when a new element is added, it removes the first one, shifts the others a long and then addeds the new one elements at the end.

    Just wondering if someone could help explain to me the best way to do this.

    Thanks
    In times of war, shoot!

  2. #2
    Daring to be a guru
    Join Date
    Dec 2007
    Posts
    14
    i think so this will help you....

    Code:
    for(i=0,j=1;i<n;i++,j++)
    {
    
      if(i==(n-1))
       {
        arr[i]=new_number;
       }
      else
       arr[i]=arr[j];
     }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Much cleaner and easier code:
    Code:
    int main()
    {
    	int i, j, length = 5, elem_to_shift = length;
    	int myarray[5] = { 1, 2, 3, 4, 5 };
    	
    	for (i = 0; i < 2; i++)
    	{
    		for (j = 0; j < elem_to_shift - 1; j++)
    		{
    			/* Move elements backwards */
    			myarray[j] = myarray[j + 1];
    		}
    		elem_to_shift--;
    	}
    	myarray[3] = 6;
    	myarray[4] = 7;
    }
    Or maybe
    Code:
    int main()
    {
    	int myarray[5] = { 1, 2, 3, 4, 5 };
    	memmove(myarray, &myarray[1], 4);
    	myarray[4] = 6;
    }
    The second example doesn't seem to work, though.
    Since memmove is assembly, I don't really know why it doesn't work, though technically it should.
    Last edited by Elysia; 12-10-2007 at 12:18 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    We all make mistakes. memmove copies bytes, as you might recall. The following works good.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
        int ar[] = { 1, 2, 3, 4, 5, };
        const int N = sizeof ar / sizeof ar[0];
        int newest = 2;
        int i;
    
        memmove( ar, &ar[N - newest - 1], (N - newest) * sizeof ar[0] );
        ar[3] = 6;
        ar[4] = 7;
    
        for ( i = 0; i < N; i++ ) printf( "%d\n", ar[i] );
        return 0;
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Much cleaner and easier code:
    Yes, and unfortunately much more incorrect. I know we've recently had somone who was moving elements over in an array and they needed to copy them backwards, but in this instance, the items are being moved towards, the lower index (to the left). So you actually have to specifically NOT copy them backwards for it to work in this case.
    Not to mention the code you posted suffers from buffer underrun as well.

    If moving to the left, always copy forwards, if moving to the right, always copy backwards. This is exactly what memmove does.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is it? Take a look again
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 08:05 AM
  2. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  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