Thread: shifting 1D array items down

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question shifting 1D array items down

    I am trying the following but I think its not getting to the last, item or something, can someone have a look and see if im right for the shifting of the array (red bit):

    Code:
    int smallItems=0;
    	for(listi=0; listi<numOfItems; listi++)
    	{
    		for (j=0;j<height;j++) 
    		{
    	    		for (i=0;i<width;i++) 
    			{
    				if(array2[j][i] == objects[listi])
    				{
    					smallItems++;			
    				}	
    			}
    		}
    		if(smallItems < 525)
    		{
    			for (j=0;j<height;j++) 
    			{
    	    			for (i=0;i<width;i++) 
    				{
    					if(array2[j][i] == objects[listi])
    						array2[j][i] = 0;
    				}
    			}
    			for(i=listi; i<numOfItems-1; i++)
    			{
    				objects[listi] = objects[listi+1];
    			}
    			listi--;
    			numOfItems--;
    			
    		}
    		printf("SmallItems: %d, listi: %d\n", smallItems, listi);
    		smallItems = 0;
    	}

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    By down do you mean left or right ?

    This will shift left and overwrite the first element -

    eg:

    0 1 2 3
    ^
    1 1 2 3
    ^
    1 2 2 3
    ^
    1 2 3 3
    ...

    What exactly are you wanting to do ?
    Spidey out!

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    i mean from right to left
    so
    Code:
    1 2 3 4
        ^
    should become
    1 2 4
    
    given 3 is deleted
    Basically objects[] contains an array of variables and in this test case say 4. Then i perform some function to determine if I have to get rid of an item in objects[] in which case i shift items down and take 1 of numOfItems(which contains how many items are in objects[])


    For some reason numOfItems at the beginning of the for loop is 4, but then at the end after it its 0 when it should be 1
    Last edited by taurus; 09-29-2009 at 05:16 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's say our array has only: 1 2 3 4
    You want to get rid of the 3: 1 2 4 ?
    What's going to end up in the last spot? There's nothing that you can put there that will mean "empty" really. You need to sort of figure out what you want to happen to that last spot.


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

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your loop uses listi instead of i. Oops.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  2. malloc 1d string array
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-07-2008, 09:20 AM
  3. Array shifting help
    By viclaster in forum C Programming
    Replies: 13
    Last Post: 11-16-2003, 07:50 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM