Thread: Cycling through an array

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Cycling through an array

    I know that if we want to cycle through an array (going from end to the starting) we can use "%" operator.Bu what must I do if I want to cycle in the opposite direction.For example if i have an array with 5 elements (array[5]) and when i have -2 in hand, i want to rearrange this number and i want to access to array[4] of the array.
    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    % doesn't seem like it would be a great operator to use for cycling through an array. Do you actually mean something besides cycling? Your question is pretty unclear (e.g. what does "-2 in hand" mean?) The common operators for cycling through an array are -- for end to start, and ++ for start to end.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    When you have an array of 5 elements and in your program and if you want to cycle through your array, don't you use "%" operator.For example if you have 6 and array[6%5]--> array[1] so that you will be still in the bounds.I want to do the reverse.If i have -1, i want to rearrange the number so that i can use this number to access array[5]...

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about: array[5 - (abs(-1) % 5)]?
    Or more generally: array[array_size - (abs(index) % array_size)]

    For -1 you get 4.
    For -2 you get 3.
    For -3 you get 2.
    For -4 you get 1.
    For -5 you get 0.
    For -6 you get 4.
    and so on...
    If you understand what you're doing, you're not learning anything.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(const int *array, int size, int index)
    {
       int i = index % size;
       if ( i < 0 )
       {
          i += size;
       }
       printf("%2d: array[%d] = %d\n", index, i, array[i]);
    }
    
    int main(void)
    {
       const int value[] = {10,20,30,40,50,60};
       int i;
       for ( i = -8; i < 8; ++i )
       {
          foo(value, sizeof value / sizeof *value, i);
       }
       return 0;
    }
    
    /* my output
    -7: array[5] = 60
    -6: array[0] = 10
    -5: array[1] = 20
    -4: array[2] = 30
    -3: array[3] = 40
    -2: array[4] = 50
    -1: array[5] = 60
     0: array[0] = 10
     1: array[1] = 20
     2: array[2] = 30
     3: array[3] = 40
     4: array[4] = 50
     5: array[5] = 60
     6: array[0] = 10
     7: array[1] = 20
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM