title: Go through the array back and forth
body: <p>We have an array of some length(say 3) and some counting sequence: <code>0,1,2,3,4,...</code> to infinity. Out of that input sequence we need to produce sequence that will traverse through array back and forth, like that: <code>0,1,2,1,0,1,2,1,0,...</code> and so on for a <code>length=3</code>.</p>
  
  <p>I think this task is a very common one in a many programming books, but i was not able to find standard solution, so i created my own solution. Are there any other more efficient and elegant solution, because i dont like my solution??? </p>
  
  <pre><code>#define LENGTH 5
  int main()
  {
     char arr[LENGTH] = {'a','b','c','d','e'};
     int i;
     int base=0;
     for(i=0;i&lt;100;i++){ 
          if(i%(LENGTH-1)==0){ 
              if(base==0) base=LENGTH-1;
              else base =0;
          }
      int j = abs(base-i%(LENGTH-1)); 
      printf("%c ",arr[j]); 
     }
  }
  </code></pre>
  
  <p>Java code (for your convenience):</p>
  
  <pre><code>public static void traverse(){
          char arr[] = {'a','b','c','d','e'};
          int base=0;
          for(int i=0;i&lt;100;i++){ 
              if(i%(arr.length-1)==0){ 
              if(base==0) base=arr.length-1;
              else base =0;
              }
          int j = Math.abs(base-i%(arr.length-1)); 
          System.out.println(arr[j]+" ");
          }
      }
  </code></pre>