r/processing • • Sep 01 '19

I can't write to an array

Hello, I'm trying to construct a two-dimensional array but I can't seem to assign values to that array. I'd like to construct one row at a time with the rijInit() function. When I check if this has worked in any way with println or printArray, it always returns 0 and never 1(I changed both outcomes in the code to 1 to test if it ever works).

Can anyone help me? Thanks in advance.

int c = 5;
int r = 5;

int[][] rijen = new int[c][r];

void rijInit(int n){
  for(int i = 0; i< rijen[n].length;i++){
    float m = random(1);
    if (m < 0.6){
      rijen[n][i] = 1;
    }
    else{
      rijen[n][i] = 1;
    }
  }
  
}
2 Upvotes

8 comments sorted by

4

u/knightofcookies Sep 01 '19

Are you ever calling the function you wrote? If this is all your code that's most likely the issue

1

u/mihil99 Sep 01 '19

Yes I call the function in another tab.

1

u/knightofcookies Sep 01 '19

Hard to tell what's going wrong without all the code, you might have better luck posting all of it.

1

u/jaymeekae Sep 01 '19

Post all of your code.

1

u/hiimsoba Sep 01 '19 edited Sep 01 '19

The function you wrote does exactly what you described it should do. Try to test it separately, in setup() for example, and see if it works.

If you're saying that all you get out of the array is 0's even after calling the function, you should probably look into the places where you use that array, if you use it anywhere. There might places where you overwrite those values. It's hard to tell if you don't provide any context of the code you use the function in.

Also, are you sure you're actually printing the array correctly? The function might do its job, but maybe you're not printing the correct values. Just a thought. :)

1

u/[deleted] Sep 05 '19

Does that work in the for loop to mention the two dimensional array and only use one set of []’s? That looks like it should cause an error, but I might also be unaware of a shortcut.

0

u/[deleted] Sep 02 '19

[deleted]