We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › fill arrays with patterns
Page Index Toggle Pages: 1
fill arrays with patterns (Read 481 times)
fill arrays with patterns
Jun 17th, 2009, 9:57am
 
hi i need to create an array with numbers , i need  the array to  have 2 zero numbers after 2 random numbers   . how can i do this? heres an example of what i need: the first 2 numbers are random , then the following 2 numbers are zero, then the following 2 numbers are random, then the following 2 numbers are zero, etc.

I need that the array can be scalable, any idea of how to doing this?




       numbers[0] = 3;        
       numbers[1] = 7;  
       numbers[2] = 0;  
       numbers[3] = 0;  
       numbers[4] = 2;    
       numbers[5] = 12;
       numbers[6] = 0;  
       numbers[7] = 0;  
       numbers[8] = 4;    
       numbers[9] = 6;
       numbers[10] = 0;
       numbers[11] = 0;
       numbers[12] = 2;
       numbers[13] = 8;
       numbers[14] = 0;
       numbers[15] = 0;
Re: fill arrays with patterns
Reply #1 - Jun 17th, 2009, 12:07pm
 
Here is what an array would look like, if you don't know what size to make it up front, you could use an ArrayList.
Code:

int[] arrayname = new int[16]; //set size of array to 16 (needs to be increment of 4 since we set 4 values at a time)

 for (int i = 0; i < arrayname.length; i++) {
   arrayname[i] = (int)random(0,20); //random integer between 0-20
   i++;
   arrayname[i] = (int)random(0,20); //random integer between 0-20
   i++;
   arrayname[i] = 0;
   i++;
   arrayname[i] = 0;
 }

 for (int i = 0; i < arrayname.length; i++) {
   println(arrayname[i]);
 }
Re: fill arrays with patterns
Reply #2 - Jun 17th, 2009, 12:26pm
 
cool thanks it works perfect, but is it possible to do the same with % ?
Re: fill arrays with patterns
Reply #3 - Jun 17th, 2009, 12:43pm
 
Smells like homework... but I will bite.
Code:
  for (int i = 0; i < arrayname.length; i++) {
if (i % 4 < 2)
{
arrayname[i] = (int)random(0,20); //random integer between 0-20
}
}
Page Index Toggle Pages: 1