Shifting a larger array through a smaller array.

I am trying to shift an array of 30 elements through an array of 6 elements and am running into a mental road block as how to accomplish this.

for example.

These are global arrays.

int counter[6] = {42,42,42,42,42,42};
int helper[30] = {1,2,3,4,5,42,42,42,5,4,3,2,1,42,42,1,2,3,4,7,42,42,42,42,4,2,6,2,5,9};

so for every shift the array would look like this

initial state: counter[] = {42,42,42,42,42,42};

state 1: counter[] = {42,42,42,42,42,1};
state 2: counter[] = {42,42,42,42,1,2};
state 3: counter[] = {42,42,42,1,2,3};
state 4: counter[] = {42,42,1,2,3,4};
state 5: counter[] = {42,1,2,3,4,5};
state 6: counter[] = {1,2,3,4,5,42};
state 6: counter[] = {2,3,4,5,42,42};
.
.
.
state n: counter[] = {4,2,6,2,5,9};

It should keep shifting till it got to the last element in helper[] then start shifting again from the beginning of helper;

Any help will be greatly appreciated.

Here's an easy way:

// untested code

static int cursor = 0;

  counter[0] = counter[1];
  counter[1] = counter[2];
  counter[2] = counter[3];
  counter[3] = counter[4];
  counter[4] = counter[5];
  counter[5] = helper[cursor];
  ++cursor;
  if (cursor >= 30)
    cursor = 0;

a couple of for loops would do the job

Well, one way is to write a loop that shifts the current content one entry and then adds the new entry. The code for that would be pretty simple. But is it actually necessary to do it at all? The counter array looks as if it is just used as a window into the helper array, so you could replace counter with a pointer to the current part of helper. Something like this:

int helper[] = {42,42,42,42,42,42,1,2,3,4,5,42,42,42,5,4,3,2,1,42,42,1,2,3,4,7,42,42,42,42,4,2,6,2,5,9};
int *counter = &helper[0]; // counter[0] .. counter[5] contains 42, 42, 42, 42, 42, 42

counter++; // counter points to helper[1]; counter[0] .. counter[5] contains 42,42,42,42,42,1

counter++; // counter points to helper[2]; counter[0] .. counter[5] contains 42,42,42,42,1,2

You can either assign an address to counter directly as counter = &helper[0] or increment/decrement as counter++, counter--.

This code works by virtue of the fact that an array is syntactically equivalent to a pointer to the first element of the array, and the counter++ and counter-- is some pointer arithmetic that conveniently does exactly what you need to reference adjacent values such as those stored in an array.

Thank you both for the help.

Both methods worked.

Wouldn't it be much simpler to have a "virtual" short array that is just an index into the bigger one - such as for 2 to 7 or for 4 to 9

If you want the small array to have the values 42, 42 etc just prepend them to the big array.

...R

for (int n2=1; n2<=sizeof(counter); n2++)
         counter[n2-1]=counter[n2];          
      counter[0]=helper[n];

That's it!.

int counter[6] = {42,42,42,42,42,42};
int helper[30] = {1,2,3,4,5,42,42,42,5,4,3,2,1,42,42,1,2,3,4,7,42,42,42,42,4,2,6,2,5,9};

void debug()
{
  for (int nn=0; nn<=6; nn++)
    {               
      Serial.print(counter[nn]);
      delay(224);             
      Serial.print(',');
      delay(44);
     }    
    delay(44);
    Serial.println("");       
      
}

void setup()
{
 Serial.begin(9600);
 debug();
 for (int n=0; n<=sizeof(helper); n++)
  {   
      for (int n2=1; n2<=sizeof(counter); n2++)
         counter[n2-1]=counter[n2];          
      counter[0]=helper[n];  
      debug();
    }  
 }    
     
//   for int n2=0 


void loop() 
 {
   
 }

You could always use memmove().

  static byte cursor1 = 0;     // cursor is a reserved word?!?

  memmove(counter, counter + 1, 5 * sizeof(int));
  counter[5] = helper[cursor1];
  if (++cursor1 >= 30)
    cursor1 = 0;

But I think PeterH's idea is brilliant. I started to go down the same road, but abandoned it because of the boundary case at the beginning. The idea to prepend the beginning state to the array is what makes it work. To finish it out, you have to append some of the beginning of the array to the end as well, but I think it is doable:

int helper[41] = {42,42,42,42,42,42,1,2,3,4,5,42,42,42,5,4,3,2,1,42,42,1,2,3,4,7,42,42,42,42,4,2,6,2,5,9,1,2,3,4,5};
int *counter = helper;

...

  if (++counter >= helper + 36)
    counter = helper + 6;