Thread: shifting in a circle

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    shifting in a circle

    Hi,
    in this code I'm trying to make a function that will perform a circulated shift of the numbers in an array, for example:
    123 become 312
    I have a bug in the code that I don't find !
    (the user give the times for shifting)
    thank for the helpers

    Code:
    #include<stdio.h>
    
    
    
    void shift_to_end(int array[],int size)
    {
    	int i = 0, temp = 0;
    	for(i = size; i > 0 ; --i)
    	{	
    		temp = array[i];
    		array[i] = array[i-1];
    		array[0] = temp;
    	}
    
    }
    
    
    
    #define ARRAY_SIZE 10
    
    int main()
    {
    	int numbers[ARRAY_SIZE];
    	int i = 0, shift = 0 ;
    
       printf("Please enter 10 numbers:\n");
    
       for (i = 0; i < ARRAY_SIZE ; ++i)
          scanf(" %d", &numbers[i]);
    
       printf("please enter times to shift\n");
    		scanf("%d", &shift);
    
    for (i=1; i <= shift; ++i);
    	shift_to_end(numbers, ARRAY_SIZE);
       
    /* print the shifted array */
        for (i = 0; i < ARRAY_SIZE; ++i)
            printf("%d", numbers[i]);
    
        return 0;
    
    	
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    From a quick glance,

    for(i = size; i > 0 ; --i)
    ...
    temp = array[i];
    Array indices start at 0, and end at size-1.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    In the first time the for-loop is executed, you just loose array[0].
    The best thing to do is to take pencil and paper and do the loop by hand

    If you take size == 10 then size - 1 = 9

    If i == 9 (this means: in the first iteration of the loop):
    temp = array[i] = 8 (for example, if 8 was the 10-th value)
    array[i] = array[i-1] = 7 (for example, if 7 was the 9-th value)
    array[0] = temp = 8

    This means that array[0] is overwritten before its value is temporarily stored.
    Take a pencil and do the loop by hand (write down the value of each variable). You will find out what's going wrong.
    Last edited by hilarius; 12-01-2009 at 01:39 AM.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    To make it a bit more clear: suppose for simplicity that ARRAY_SIZE is 5 and the initial array is {1, 2, 3, 4, 5}

    Then after the first iteration you have the following array:

    Code:
    {5, 2, 3, 4, 4}
    You see, the 1 is gone. And the 4 on the fourth position is also still there.
    Last edited by hilarius; 12-01-2009 at 01:56 AM. Reason: typos

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    thank u I did it!

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    It's easier, cleaner, more convenient, using two arrays; but requires more memory.

    In pseudo;

    A[3] = {1, 2, 3}
    B[3]
    N = 3
    ShiftLen = 1

    For k = 0, N-1
    B[k] = A[(k + ShiftLen)%N];

    ---

    ShiftLen = 0; B = { 1, 2, 3}
    ShiftLen = 1; B = { 2, 3, 1}
    ShiftLen = 2; B = { 3, 1, 2}
    ShiftLen = 3; B = { 1, 2, 3}

    ---

    I've had to implement a continuously rotating array for a visual effect I was developing, and the rate of rotations changed based on events. For that I just coupled the arrays together like such; A[2][N]. I would use a variable mod 2 to flip-flop the array being referenced. For rotations one iteration would be A[0][N] = A[1][shift % N], the next iteration A[1][N] = A[0][shift % N].

    I know how awkward that seems at first, but it's fast and drastically easier to work with. Going the way without coupled arrays you're going to have to create loops that partition the array in to N-size segment loops, and then you have to deal with funky 'boundary' conditions so everything wraps correctly around the end/beginning (which means a host of crappy tmp vars and unintuitive tricks).

    Sacrifice memory for agility, sometimes it is freaking worth it.... of course it also depends on your application.
    Last edited by since; 12-01-2009 at 03:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circle problem
    By gunghomiller in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:40 PM
  2. Drawing circle into a bitmap file
    By brokensail in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2006, 01:26 AM
  3. Half of the circle
    By hdragon in forum Game Programming
    Replies: 14
    Last Post: 03-10-2006, 10:15 PM
  4. circle filling
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 05:37 AM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM