About for-loop need help please

Hi all.
I have:

int xA[] = {11, 22, 33, 44, 55};
uint16_t yA[5];

I got the expected result by the sketch below:

yA[0]=11
yA[1]=22
yA[2]=33
yA[3]=44
yA[4]=55
int i = 0;
int xA[] = {11, 22, 33, 44, 55};
uint16_t yA[5];

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  delay(1000);
  Serial.println("start");

  for ( i = 0; i < 5; i++)
  {
    yA[i] = xA[i];
    delay(10);
    Serial.print("yA["); Serial.print(i); Serial.print("]="); Serial.println(yA[i]);
    delay(300);
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}

question is how to get:

yA[0]=22
yA[1]=33
yA[2]=44
yA[3]=55
yA[4]=11

and:

yA[0]=33
yA[1]=44
yA[2]=55
yA[3]=11
yA[4]=22

etc. loop?

Thanks
Adam

Copy one array to another with circular shift:

 for ( i = 0; i < 5; i++)
  {
    yA[i] = xA[ (i + shift) %5];
   
  }

This will be :

for shift = 1
and

if shift = 2

1 Like

Great!
it works.
Thank you b707.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.