Looping through an array?

If I have an array with 14 elements, Is there a way to loop through the array so that when I get to array[13], it automatically goes back to array[0] up to array [13] and loop back to array[0] etc etc?

I was thinking modulo, but I am not sure where I would place that. Then I was thinking maybe I can make the array 15 elements, and when it reaches [15], set that array[15] = array[0] and somehow get it to loop again.

This array will be in a ‘for loop’ where I will be using array[i] and saying array[i] ++; I want it to continuously add +1 until I reach the “max”, or whatever i < max.

I am very new to processing, any help would be greatly appreciated! I don’t want to paste my code as it is not written well and is quite lengthy!

1 Like

right, that might even stop readers from helping,
but what you should do is to make a extra small program
( complete / run-able for the problem only )
where we can discuss your question with.

and also give it a nice format, like

// https://discourse.processing.org/t/looping-through-an-array/13078
//_________________________________________________TEST array def
int[] numbers = { 90, 150, 30, 7, 8, 0, 22, 44, 36, 52, 82, 31, 63, 39 };
//_________________________________________________SETUP
void setup() {
  printArray(numbers);
}
//_________________________________________________DRAW
void draw() {
  for ( int i = 0; i < 14; i++ ) println("_"+i+"_: "+numbers[i]);
  println("next draw loop");
}

Okay, sorry, I put together a really simple code. So in my code, int startingPoint will change depending on some other things so I want to be sure that when startingPoint is greater than 9, that it will loop the array back to [0] and add 1 to that. Right now, my program just crashes when I have a starting number of 10 or greater.

Here is the code:

int[] myArray = new int [14];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index

void initialNumbers()
{
  for (int i = 0; i < myArray.length; i ++)
  {
    myArray[i] = startingAmount; //sets all to starting amount
  }
}

void addNumber()
{
  for (int i = 0; i < startingAmount+1; i ++)
  { 
    myArray[startingPoint+i] ++;
  }
  myArray[startingPoint] = 0;
}

void setup()
{
  initialNumbers();
  addNumber(); 
}

yes, array what is long 14
has a possible index from 0 … 13
so a

  for (int i = 0; i < startingAmount; i++) {

works, still the result is questionable
WHY YOU NOT PRINT ANYTHING

int[] myArray = new int [14];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index

void initialNumbers() {
  for (int i = 0; i < myArray.length; i ++)  myArray[i] = startingAmount; //sets all to starting amount
}

void addNumber() {
//  for (int i = 0; i < startingAmount+1; i++) {
  for (int i = 0; i < startingAmount; i++) {
    println("add "+i);
    myArray[startingPoint+i]++;
  }
  myArray[startingPoint] = 0;
}

void setup() {
  initialNumbers();
  addNumber();
  println(myArray);
}

shows in console

add 0
add 1
add 2
add 3
[0] 4
[1] 4
[2] 4
[3] 4
[4] 4
[5] 4
[6] 4
[7] 4
[8] 4
[9] 4
[10] 0
[11] 5
[12] 5
[13] 5

So with startingPoint = 10, I want:

[10] 0
[11] 5
[12] 5
[13] 5
[0] 5

with startingPoint = 11, I want:

[10] 4
[11] 0
[12] 5
[13] 5
[0] 5
[1] 5

I am sorry I didn’t put print in there, I am so new that even printing things confuses me :weary:

I appreciate your help though, I really do!!!

so you must roll around your pointer

int along = 14;
int[] myArray = new int [along];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index
boolean diagp = true;

void initialNumbers() {
  for (int i = 0; i < myArray.length; i ++)  myArray[i] = startingAmount; //sets all to starting amount
}

void addNumber() {
  for (int i = 0; i < startingAmount+1; i++) {
    int j = startingPoint+i;
    if ( j >= myArray.length ) j -= myArray.length;
    myArray[j]++;
    if ( diagp ) println("add i: "+i+ " j: "+j+ " myArray["+j+"]="+myArray[j]);
  }
  myArray[startingPoint] = 0;
}

void setup() {
  initialNumbers();
  addNumber();
  if ( diagp ) for ( int i = 0; i < myArray.length; i++ ) print(myArray[i]+" , ");
}

shows

add i: 0 j: 10 myArray[10]=5
add i: 1 j: 11 myArray[11]=5
add i: 2 j: 12 myArray[12]=5
add i: 3 j: 13 myArray[13]=5
add i: 4 j: 0 myArray[0]=5
5 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 0 , 5 , 5 , 5 , 
1 Like

Exactly, you don’t change the array but you change the index (the line number of the array) to make the index do what you want.

Hey There!

Modulo is the way to do it !

for int i = 0 true i++
      ar[i%ar.length]
1 Like

Thank you so much! I think I understand now. It works great when I call that function in setup, but is there a way to get this function to work in draw so I can change the startingAmount value based on other functions? When I move the function into draw, no matter what my startingAmount is, I get an error saying out of bounds

1 Like

Of course you can

Show your entire code

Otherwise we can’t know what you did wrong

I am playing around with @kll’s above code, except I moved “addNumber();” to void draw();. I guess because draw is continuously running, it keeps adding and adding, but I thought the for loop conditions would stop this!

Blockquote

1 Like

I got it you guys!!! Thanks so much for everyones help!!! I appreciate it x 1 million

1 Like

Can you post your final code example please

It looks like you are trying to create Mancala - The African Stone Game is that correct?




int along = 14;
int[] myArray = new int [along];

int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index

boolean diagp = true;

// -----------------------------------------------------------------------------------

void setup() {
  size(300, 300); 
  initialNumbers();
}

void draw() {
  addNumber();
  if ( diagp ) { 
    for ( int i = 0; i < myArray.length; i++ ) 
      print(myArray[i]+" , ");
  }//if
}

// -----------------------------------------------------------------------------------

void initialNumbers() {
  //sets all to starting amount
  for (int i = 0; i < myArray.length; i ++) 
    myArray[i] = startingAmount;
}

void addNumber() {
  //
  for (int i = 0; i < startingAmount+1; i++) {

    int j = startingPoint+i;
    if ( j >= myArray.length ) 
      j -= myArray.length;

    myArray[j]++;

    if ( diagp ) 
      println("add i: "
        +i
        + " j: "
        +j
        + " myArray["+j+"]="
        +myArray[j]);
  }//for
  myArray[startingPoint] = 0;
}
//
1 Like

Yup, you are correct!

I have never played the game but it suddenly dawned on me that the algorithm seemed familiar.

A quick google showed that although you start with the same game configuration there are several ways or sets of rules to play the game. One of them prevents you placing stones in your opponents home cup but the current algorithm does not allow for that.

Hi
Could you just enclose your for loop in an outer for loop or other looping construct? Initialise your inner loop variable to a start value computed inthe outer loop.

I’d write a very small class that does the looping interation. It only offers a constructor, taking the length of the loop, a method “iterate” and a method int value() to get the current index it is pointing on.

1 Like

Normally I would say there isn’t much more to it than using modulo – so no need, your “class” is int idx, its “next” method is idx=(idx+1)%arr.length, and that’s it.

However, Java’s % is actually the remainder, which causes a gotcha on negatives, so IF you ever wanted to step negative numbers then it might be useful to wrap a loop counter in a method or class.

/**
 * LoopCounter
 * https://discourse.processing.org/t/looping-through-an-array/13078/19
 **/
void setup() {
  LoopCount lc = new LoopCount(4);
  println(lc.val);
  println(lc.inc(1), lc.inc(1), lc.inc(1), lc.inc(1));
  println(lc.inc(2), lc.inc(2));
  println(lc.inc(7));
  println(lc.inc());
  println(lc.dec(), lc.dec(), lc.dec(), lc.dec());
  println(lc.dec(7));
}

class LoopCount {
  int max;
  int val;
  LoopCount(int max) {
    this.max = max;
  }
  LoopCount(int max, int val) {
    this.max = max;
    this.val = val;
  }
  int dec() {
    return inc(-1);
  }
  int dec(int count) {
    return inc(-count);
  }
  int inc() {
    return inc(1); 
  }
  int inc(int count) {
    this.val = ((val+count)%max + max)%max;
    return this.val;
  }
}

Output:

0
1 2 3 0
2 0
3
0
3 2 1 0
1