Code
float array[] = {
};
void setup (){
size (300,300);
array = new float[20];
for (int i = 0; i < array.length; i ++){
array[i] = random (300);
}
}
void draw (){
stroke (0,10);
for (int i = 0; i < array.length; i ++){
line(array[i], 0, array[i], height);
array[i] ++;
if (array[i] >= width){
array[i] -= width;
}
}
}
0501a - Arrays: Declare, Allocate, Assign, Retrieve, Re-Assign
Statement:In this assignment you will code all 5 important steps in creating and using an array. This will be a small time-based program that uses setup() and draw().
Declare a globally-scoped float array. Then, in setup(), allocate RAM so that this array can store up to 20 numbers. Populate this array with random floats between 0 and 300. In a canvas which is 300x100 pixels, draw 20 vertical lines which extend from the top to the bottom of the canvas, and whose x-coordinates are fetched from the elements of the array.
Demonstrate that the positions of the lines are really stored in this array, by changing the values in this array over time. On each frame of animation (i.e., on each pass through the draw() function), add 1 to each member of your array so that all of the lines move slowly to the right.
If a given value stored in the array is greater than the width of the canvas, subtract the width of the canvas from that member of the array so that the line stays visible.