We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Good afternoon all.
I'm new to Processing and this forum.
We've just started Processing in one of my University modules, we were set three challenges the number one and two were fine but the third I've been racking my brain over for a number of days now, I'm sure its pretty simple but I'm cleary missing it, could anybody help out on here for me. The challenge was as follows:
//challenge 3 - create a fixed array of 20 numbers and use a while loop to set them to random numbers in the setup method use a while loop like the one above to print them out to the screen.
Here is the code which I've used so far:
int[] randomNumbers = new int[20];
void setup(){
size(640,480);
frameRate(5);
int counter = 0;
while(counter < 20 ){
int randomNumbers = (int)random(2,500);
counter = counter + 1;
}
}
void draw(){
background(0);
fill(255);
stroke(255);
int counter = 0;
// this is a local variable - it only exists within the containing curly brackets
while(counter < 20){ //this is a while loop
//the code between these curly brackets
//will keep repeating until counter > 20
int yPos = 20 + counter * 20; //look another local variable - it only exists inside the while loop
text("counter = " + randomNumbers,20, yPos );
counter = counter + 1; //increment the counter variable
}
}
Cheers for any help
Gragnit
Answers
you need to use the variable 'counter' that you have created in setup() as the index of the array to access it. for example:
a quicker way of writing the final line is simply: counter++;
http://processing.org/reference/Array.html
It sounds like you don't need that draw function at all.
It also sounds like you need to assign the indexes of the array to values. Right now you're assigning a single int randomNumbers to be a random value 20 times, but you don't do anything with that value.
Here is the Processing tutorial on array access: http://www.processing.org/reference/arrayaccess.html And here is one I wrote: http://StaticVoidGames.com/tutorials/basicConcepts/Arrays.jsp